Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing files in a specific "folder" of a AWS S3 bucket

I need to list all files contained in a certain folder contained in my S3 bucket.

The folder structure is the following

/my-bucket/users/<user-id>/contacts/<contact-id> 

I have files related to users and files related to a certain user's contact. I need to list both.

To list files I'm using this code:

ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName("my-bucket")                 .withPrefix("some-prefix").withDelimiter("/"); ObjectListing objects = transferManager.getAmazonS3Client().listObjects(listObjectsRequest); 

To list a certain user's files I'm using this prefix:

users/<user-id>/

and I'm correctly getting all files in the directory excluding contacts subdirectory, for example:

users/<user-id>/file1.txt users/<user-id>/file2.txt users/<user-id>/file3.txt 

To list a certain user contact's files instead I'm using this prefix:

users/<user-id>/contacts/<contact-id>/

but in this case I'm getting also the directory itself as a returned object:

users/<user-id>/contacts/<contact-id>/file1.txt users/<user-id>/contacts/<contact-id>/file2.txt users/<user-id>/contacts/<contact-id>/ 

Why am I getting this behaviour? What's different beetween the two listing requests? I need to list only files in the directory, excluding sub-directories.

like image 514
davioooh Avatar asked Jun 27 '16 10:06

davioooh


People also ask

How do I give access to a specific directory in S3 bucket?

If the IAM user and S3 bucket belong to the same AWS account, then you can grant the user access to a specific bucket folder using an IAM policy. As long as the bucket policy doesn't explicitly deny the user access to the folder, you don't need to update the bucket policy if access is granted by the IAM policy.

Can you tag a folder in S3?

Using the S3 consoleYou can also optionally navigate to a folder. In the Objects list, select the checkbox next to the names of the objects that you want to add tags to. In the Actions menu, choose Edit tags. Review the objects listed, and choose Add tags.


1 Answers

While everybody say that there are no directories and files in s3, but only objects (and buckets), which is absolutely true, I would suggest to take advantage of CommonPrefixes, described in this answer. So, you can do following to get list of "folders" (commonPrefixes) and "files" (objectSummaries):

ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucket.getName()).withPrefix(prefix).withDelimiter(DELIMITER); ListObjectsV2Result listing = s3Client.listObjectsV2(req); for (String commonPrefix : listing.getCommonPrefixes()) {         System.out.println(commonPrefix); } for (S3ObjectSummary summary: listing.getObjectSummaries()) {     System.out.println(summary.getKey()); } 

In your case, for objectSummaries (files) it should return (in case of correct prefix):
users/user-id/contacts/contact-id/file1.txt
users/user-id/contacts/contact-id/file2.txt

for commonPrefixes:
users/user-id/contacts/contact-id/

Reference: https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html

like image 118
Vic K Avatar answered Oct 01 '22 02:10

Vic K