[英]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.