Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 - Listing all objects inside a folder without the prefix

I'm having problems retrieving all objects(filenames) inside a folder in AWS S3. Here's my code:

ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
            .withBucketName(bucket)
            .withPrefix(folderName + "/")
            .withMarker(folderName + "/")

    ObjectListing objectListing = amazonWebService.s3.listObjects(listObjectsRequest)

    for (S3ObjectSummary summary : objectListing.getObjectSummaries()) {
        print summary.getKey()
    }

It returns the correct object but with the prefix in it, e.g. foldename/filename

I know I can just use java perhaps substring to exclude the prefix but I just wanted to know if there is a method for it in AWS SDK.

like image 358
Marz Avatar asked Apr 22 '14 11:04

Marz


People also ask

Are folders considered objects in S3?

Object. Every file that is stored in s3 is considered as an object. Each Amazon S3 object has file content, key (file name with path), and metadata.

What is S3 path prefix?

A key prefix is a string of characters that can be the complete path in front of the object name (including the bucket name). For example, if an object (123. txt) is stored as BucketName/Project/WordFiles/123. txt, the prefix might be “BucketName/Project/WordFiles/123.

Is it better to have multiple S3 buckets or one bucket with sub folders?

Simpler Permission with Multiple Buckets If the images are used in different use cases, using multiple buckets will simplify the permissions model, since you can give clients/users bucket level permissions instead of directory level permissions.

How can I get only one level of objects in a S3 bucket?

How can I get only one level of objects in a S3 bucket? To list only the root level objects in the bucket, you send a GET request on the bucket with the slash ( / ) delimiter character. In response, Amazon S3 returns the sample.


1 Answers

This code help me to find sub-directory of my bucket.

Example :- "Testing" is a my bucket name , inside that contain "[email protected] " folder then its contain "IMAGE" folder in which contain image files.

     ArrayList<String> transferRecord = new ArrayList<>();    

     ListObjectsRequest listObjectsRequest =
                            new ListObjectsRequest()
                                    .withBucketName(Constants.BUCKET_NAME)
                                    .withPrefix("[email protected]" + "/IMAGE");

      ObjectListing objects = s3.listObjects(listObjectsRequest);
        for (;;) {
                    List<S3ObjectSummary> summaries = 
                    objects.getObjectSummaries();
                        if (summaries.size() < 1) {
                            break;
                        }

                       for(int i=0;i<summaries.size();i++){
                            ArrayList<String> file = new ArrayList<>();

                            file.add(summaries.get(i).getKey());
                            transferRecord.add(file);
                        }

                        objects = s3.listNextBatchOfObjects(objects);
               }

I hope this helps you.

like image 179
iamkdblue Avatar answered Oct 12 '22 23:10

iamkdblue