Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Absolute Path of Each S3 Object in Bucket

Tags:

java

amazon-s3

Given an AWSS3Client, how can I get a complete list of all S3 Objects' paths?

Example:

Bucket Name: foo has 5 objects

  • foo/bip/baz
  • foo/bip/bap
  • foo/bar/1
  • foo/bar/2
  • foo/1234

I'd like to get a List[String] consisting of those 5 items.

How can I do this?

like image 338
Kevin Meredith Avatar asked Nov 24 '25 18:11

Kevin Meredith


1 Answers

To do this, you can call

listObjects(bucketName).getObjectSummaries() 

on the AmazonS3Client object, then iterate over that list of object summaries, calling getKey() on each one.

(The listObjects() method returns an ObjectListing, and in turn the getObjectSummaries() method returns a List<S3ObjectSummary> - and each S3ObjectSummary contains the key!)

If there are lots of objects, you also have to deal with the case that the ObjectListing is truncated, e.g. by using listNextBatchOfObjects(). See also ObjectListing.isTruncated() (javadoc).

Finally, just prepend bucketName+ "/" to each key.

like image 178
DNA Avatar answered Nov 27 '25 07:11

DNA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!