简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM