Downloading Objects from Amazon S3 using the AWS SDK [API V2] for Ruby


Set bellow variables in your project or as environment variables or whatever way you wanted.


AWS_ACCESS_KEY_ID = 'S3 bucket access key id'
AWS_SECRET_ACCESS_KEY= 'S3 bucket secret access key'
AWS_REGION = 's3 bucket region'
AWS_BUCKET= 'bucket name'

Follow any of two given ways to download objects to your local system.


s3 = Aws::S3::Client.new
s3.list_objects(bucket: 'AWS_BUCKET NAME HERE').each do |response|
  response.contents.each do |obj|
    File.open("#{Rails.root}/#{obj.key}", 'wb') do |file|
      s3.get_object( bucket: 'AWS_BUCKET NAME HERE', key: obj.key , response_target: file)
    end
  end
end

s3 = Aws::S3::Client.new
bucket = Aws::S3::Bucket.new('AWS_BUCKET NAME HERE')
bucket.objects.each do |obj|
  File.open("#{Rails.root}/#{obj.key}", 'wb') do |file|
    s3.get_object( bucket:ENV[:AWS_BUCKET], key: obj.key , response_target: file)
  end
end

get_object-instance_method V2 documentation
Official AWS SDK GEM FOR RUBY
For More Interesting Things

Advertisements

I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

Posted in amazon, S3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

StackOverFlow
Categories
Archives
%d bloggers like this: