list_objects_v2() sometimes returns first key value incorrectly
<p>I am currently writing a Python script using boto3 that opens an S3 folder (the prefix) in a bucket and prints out the contents of that folder (XML pointer filenames). The code I have for that is this:</p>

<pre><code>def getXMLFileName(self, bucketName, prefix): 
    session = boto3.Session(profile_name=self.profileName)
    s3client = session.client('s3')

try:
    for item in s3client.list_objects_v2(Bucket = bucketName, Prefix = prefix, MaxKeys = 5)['Contents']:
       print item['Key']
</code></pre>

<p>The following code does work. It will print something similar to:</p>

<pre><code> PrefixName/This.Is.The.XML.Filename1.xml
 PrefixName/This.Is.The.XML.Filename2.xml
 PrefixName/This.Is.The.XML.Filename3.xml
 PrefixName/This.Is.The.XML.Filename4.xml
 PrefixName/This.Is.The.XML.Filename5.xml
</code></pre>

<p>However, depending on which prefix I enter, some will print what I have above, which is correct, but other times it will only print out the prefix name for the first item (i.e. PrefixName/), and then it prints out correctly the rest of the files. So in this case, it prints:</p>

<pre><code> PrefixName/
 PrefixName/This.Is.The.XML.Filename1.xml
 PrefixName/This.Is.The.XML.Filename2.xml
 PrefixName/This.Is.The.XML.Filename3.xml
 PrefixName/This.Is.The.XML.Filename4.xml
</code></pre>

<p>I'm not really sure why it's doing that. I've spent at least 2 days trying to figure out why it's returning a 'null' filename for certain bucket folders, but others it works. Not sure if it's relevant, but the prefixes that DO work were all uploaded to S3 on the same day, and the ones that DON'T work were all uploaded a different day. Maybe it's a bucket permissions issue? I'm really not sure...</p>

<p>I have figured out a way around this issue by adding an if statement to the for loop:</p>

<pre><code>if item['Size'] == 0:
    pass
else:
    print item['Key']
</code></pre>

<p>However, I just want to know WHY it's outputting like that. </p>

<p>Thanks in advance!</p>

<p>Directories aren't real in S3, however, when you create your S3 dataset many tools will create a metadata construct that roughly maps to a directory on a regular file system. </p>

<p>When you query using boto you will see those metadata constructs if they were created by the tool you used to upload.</p>

python amazon-s3 boto3