Surya Teja
Surya Teja

Reputation: 31

S3 ListObjectsV2 api call not returning contents

According to the ListObjectsV2 - Amazon Simple Storage Service documentation, when I specify a Prefix and a Delimiter, I should get a contents element in the response with an ETag for the prefix.

<Contents>
    <Key>photos/2006/</Key>
    <LastModified>2016-04-30T23:51:29.000Z</LastModified>
    <ETag>"d41d8cd98f00b204e9800998ecf8427e"</ETag>
    <Size>0</Size>
    <StorageClass>STANDARD</StorageClass>
</Contents>

I have tried to run this using the python sdk (boto3).

client.list_objects_v2(Bucket='bucketname', Prefix = "folder1-folder2-", Delimiter = "-")

But in the response dict, I dont find a contents key. All the others fields as per the response in the example are present.

dict_keys(['ResponseMetadata', 'IsTruncated', 'Name', 'Prefix', 'Delimiter', 'MaxKeys', 'CommonPrefixes', 'EncodingType', 'KeyCount'])

Is this something which is no longer in the response of the API call. Or is this something the SDK doenst show. And a follow up question, if it is something on the sdk side, how do I make an api call that returns me this field.

Upvotes: 1

Views: 10470

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 270104

When a Prefix and a Delimiter is provided, the directories within that Prefix are returned in CommonPrefixes.

So, if there is an object called folder1-folder2-folder3-file.txt, then your return response should contain a CommonPrefixes list that includes folder3-.

Since you are using boto3, it's easier to look at the boto3 documentation for list_objects_v2(). It shows how the fields are provided in the response.

You can access values like this:

response = s3_client.list_objects_v2(Bucket='bucketname', Prefix = "folder1-folder2-", Delimiter = "-")

# Objects
for object in response['Contents']:
  print(object['Key'])

# Folders
for folder in response['CommonPrefixes']:
  print(folder['Prefix'])

When a user clicks Create Folder in the Amazon S3 management console, it creates a zero-length object with the same name as the 'folder'. This is because Amazon S3 does not actually use folders, but it can simulate them via Delimiter and CommonPrefixes. By creating a zero-length object, it forces that folder name to appear as a CommonPrefix. It also causes the zero-length object itself to appear as an object in the list_objects() API call.

Upvotes: 5

Related Questions