Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 how to list files in a “folder”

I set the key of files in Amazon S3 to be folder\filename. Is there a way to get all the files under a "folder" (search files by regex)?

like image 220
Yituo Avatar asked Jan 27 '23 23:01

Yituo


2 Answers

You tagged your question with aws-sdk but did not mention a language, so I'll use Python in this answer.

The list_objects_v2() command accepts a Prefix:

response = client.list_objects_v2(
    Bucket='string',
    Delimiter='string',
    EncodingType='url',
    MaxKeys=123,
    Prefix='string',
    ContinuationToken='string',
    FetchOwner=True|False,
    StartAfter='string',
    RequestPayer='requester'
)

If you set Prefix='folder/', then it will return objects within that folder.

However, it is not possible to use a Regex expression. Your program will need to filter the return list to meet your needs.

like image 149
John Rotenstein Avatar answered Feb 04 '23 13:02

John Rotenstein


I hope provided link will answer your question.

AWS S3 object listing

You can also get list of objects by using aws-cli

Type following command in terminal

aws s3 ls bucketName/folderName/

Here '/' is necessary at the end of folder name, else you will get only folder name in result.

like image 22
Asif Avatar answered Feb 04 '23 12:02

Asif