Ehud Grand
Ehud Grand

Reputation: 3703

AWS S3 .Net List Objects 3rd level only

I'm trying to list objects from my S3 bucket, 3rd level of a certain folder only:

bucket
   samples
      XXXX
        XXXX_XXXXX
        XXXX_XXXXX
      YYYY
        YYYY_YYYYY
        YYYY_YYYYY

The XXXX_XXXXX and YYYY_YYYYY folders only.

Using C#, this is my code:

 using (IAmazonS3 client = new AmazonS3Client("awsAccessKeyId", "awsSecretAccessKey", RegionEndpoint.GetBySystemName("eu-central-1")))
            {
                ListObjectsRequest request = new ListObjectsRequest
                {
                    BucketName = bucketName,
                    Prefix = "samples/",
                    Delimiter = "/"
                };
                do
                {
                    ListObjectsResponse response = client.ListObjects(request);

                    if (response.S3Objects.Count() > 0)
                    { // CODE }

The response.S3Objects is empty. If I remove the Delimiter from the Request ALL the objects are returned, and the loading time is too long. I've been following the AWS S3 docs, but it simply returns nothing. Please help me understand what is wrong. Many thanks.

Upvotes: 1

Views: 1058

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 179364

You need to be looking in CommonPrefixes, not S3Objects. CommonPrefixes gives you all of the prefixes up to the next delimiter, each of which you use to repeat the request, taking you another level deeper each time.

Upvotes: 2

Related Questions