VenkatK
VenkatK

Reputation: 1305

How can I take image from the amazon s3 bucket to my local system using rails?

I have been working with Ruby on Rails for a couple of months. My requirement is to take the images in the Amazon S3 to local system. I was able to get the objects, but not getting the image.

I have written the following code.

s3_details = YAML.load(File.read("#{Rails.root}/config/s3.yml"))    
s3 = AWS::S3.new(
  :access_key_id      => s3_details[Rails.env]['s3_access_key'],
  :secret_access_key  => s3_details[Rails.env]['s3_secret'] 
)
bucket = s3.buckets['bucket_name'] 
bucket.objects 

Can anybody help me?

Upvotes: 1

Views: 732

Answers (1)

Damien MATHIEU
Damien MATHIEU

Reputation: 32629

I would take a look at fog.
It has the big advantage of supporting several providers. So if tomorrow, you want to use something else than S3, you can very easily, with the same API.

And you can read a file very easily too.

connection = Fog::Storage.new({
  provider:              'AWS',
  aws_access_key_id:     '',
  aws_secret_access_key: ''
})
directory = connection.directories.new(key: 'bucket_name')


directory.files.each do |s3_file|
  File.open(s3_file.key, 'w') do |local_file|
    local_file.write(s3_file.body)
  end
end

The above example will connect to the bucket bucket_name, and download all files found there.

Upvotes: 2

Related Questions