简体   繁体   English

如何使用Rails将图片从Amazon s3存储桶获取到本地系统?

[英]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. 我已经在Ruby on Rails上工作了几个月。 My requirement is to take the images in the Amazon S3 to local system. 我的要求是将Amazon S3中的图像带到本地系统。 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. 因此,如果明天想使用S3以外的其他软件,则可以使用同一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. 上面的示例将连接到bucket bucket_name ,并下载在那里找到的所有文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何才能得到finerycms以显示来自Amazon S3与/ system / images…的上传图像URL? - How can I get refinerycms to display uploaded image urls from Amazon S3 versus /system/images…? 如何逐步通过我的Rails服务器从Amazon S3下载大文件 - How can I download a large file from Amazon S3 through my Rails server, progressively 即使我将存储桶公开,为什么来自Amazon S3的图像URL仍具有AWSaccesskey和有效期? - Why does my image url from Amazon S3 have AWSaccesskey and expiration even though I made the bucket public? 使用Carrierwave-aws gem,Rails4和Heroku时,为什么在Amazon S3中我的存储桶是空的 - Why is my bucket empty in Amazon S3, when using Carrierwave-aws gem, Rails4 and Heroku 如何从本地开发应用程序引用S3存储桶中的图像? - How do I reference images in my S3 bucket from local dev app? 将Amazon S3存储桶用于Rails 4中的静态资产 - Using Amazon S3 bucket for static assets in Rails 4 Amazon S3从Rails上传图像 - Amazon S3 upload Image from Rails Rails-如何创建文件(XML)并将其保存到Amazon S3存储桶中? - Rails - how to create file (XML) and save it into Amazon S3 bucket? 我应该将Amazon S3存储桶放在哪个区域? - in which region should I put my amazon s3 bucket? Rails上传到Amazon S3存储桶 - Rails uploading to Amazon S3 bucket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM