I have backups stored on Amazon S3. The folder structure is

my-bucket - db_backup - ...... - 2017.02.20.03.30.03 - db_backup.tar - 2017.02.21.03.30.04 - db_backup.tar 

I would like to make a rake task that would take the most recent backup from S3 and deploy it to me locally. for this, I decided to use the gem 'aws-sdk', '2.7.11'

The code below downloads the backup to the specified path and it works successfully.

 Aws::S3::Client.new.get_object(bucket: 'my-bucket', key: 'db_backup/2017.02.21.03.30.03/db_backup.tar', response_target: 'tmp/db_backup.tar') 

but I would like the most recent folder to be automatically selected instead of 2017.02.21.03.30.03/ please tell 2017.02.21.03.30.03/ how to get the latest folder in the db_backup/ folder

    1 answer 1

    Right in the README there are such snippets:

     # list the first two objects in a bucket resp = s3.list_objects(bucket: 'aws-sdk-core', max_keys: 2) resp.contents.each do |object| puts "#{object.key} => #{object.etag}" end # single object operations obj = bucket.object('hello') obj.put(body:'Hello World!') obj.etag obj.delete # yields one response object per API call made, this will enumerate # EVERY object in the named bucket s3.list_objects(bucket:'aws-sdk').each do |response| puts response.contents.map(&:key) end 

    I have never used this gem and S3 in general, but probably the name of the most recent backup goes something like this:

     s3.bucket("my-bucket").object("db_backup").contents.map(&:key).max 
    • one
      thank. something like this is key = Aws::S3::Resource.new.bucket('my-bucket').objects(prefix: 'db_backup').map(&:key).max Aws::S3::Client.new.get_object(bucket: 'my-bucket', key: key, response_target: 'tmp/db_backup.tar') and Aws::S3::Client.new.get_object(bucket: 'my-bucket', key: key, response_target: 'tmp/db_backup.tar') - Mikhail Lutsko