Matte3o Matte3o - 7 months ago 28
Ruby Question

Best way to return an array of files from Amazon S3 and sort by date?

I'm trying to get an array of files from an AWS S3 Bucket, then sort them by date uploaded, to choose the latest file and display it in a view.

I got to the point of doing something like this, using the Dir method in Ruby

Dir['*'].sort_by{ |f| File.ctime(f) }


However, I can't figure out how to change the working directory to the Amazon S3 directory.

Perhaps there is an easier way to do this altogether? Other suggestions would be appreciated.

Answer

If you are using the AWS SDK is fairly straightforward.

Once you have the array of files:

bucket.objects.sort_by &:last_modified
Comments