Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all files in an s3 folder using AWS-SDK gem in ruby on rails

I wanted to show a list of all files in an s3 folder so I can get all the last modified dates so I can determine what files has been changed.

I tried using objects.with_prefix('Folder1') it give me a full list but also contain Folder1.1 key

I don't know if i needed to use delimiter but I couldn't find anything how to use delimiter in aws sdk.

Thanks so much in advance!

I'm using 'aws-sdk' gem

Here is my bucket structure -Folder1 -File1 -File2 -Folder.1.1

Here is my code

bucket = s3.buckets[bucket_name] data = bucket.objects.with_prefix('Folder1/') data.each do |object|     puts "#{object.key}\t#{object.last_modified}"; end 
like image 881
user648198 Avatar asked Oct 15 '13 23:10

user648198


People also ask

How do I view files in S3 bucket?

In AWS Explorer, expand the Amazon S3 node, and double-click a bucket or open the context (right-click) menu for the bucket and choose Browse. In the Browse view of your bucket, choose Upload File or Upload Folder. In the File-Open dialog box, navigate to the files to upload, choose them, and then choose Open.

How can you download an S3 bucket including all folders and files?

aws s3 sync s3://mybucket . will download all the objects in mybucket to the current directory. This will download all of your files using a one-way sync. It will not delete any existing files in your current directory unless you specify --delete , and it won't change or delete any files on S3.

How do I see how many files are in a S3 bucket?

Open the AWS S3 console and click on your bucket's name. In the Objects tab, click the top row checkbox to select all files and folders or select the folders you want to count the files for. Click on the Actions button and select Calculate total size.


1 Answers

Too late answer but better than never.

You can do

s3_bucket.objects.with_prefix('folder_name').collect(&:key) 

According to official documentation here

Updates: SDK V3

s3 = Aws::S3::Client.new resp = client.list_objects_v2({   bucket: "BucketName", # required   prefix: "FolderName",  }) 
like image 84
kebomix Avatar answered Oct 09 '22 02:10

kebomix