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.
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.
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.
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? 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With