Rails:从URL保存文件并将其保存到Amazon S3

时间:2013-03-14 14:29:58

标签: ruby-on-rails ruby upload amazon-s3 download

从给定的URL下载文件并立即将其上传到Amazon S3的更直接的方法是什么(+将有关该文件的一些信息保存到数据库中,如名称,大小等)?

现在,我没有使用Paperclip和Carrierwave。

谢谢

1 个答案:

答案 0 :(得分:8)

直接的:

require 'open-uri'
require 's3'

amazon = S3::Service.new(access_key_id: 'KEY', secret_access_key: 'KEY')
bucket = amazon.buckets.find('image_storage')
url = 'http://www.example.com/url'
download = open(url)

file = bucket.objects.build('image.png')
file.content = (File.read download)

if file.save
  # Make a new ActiveRecord::Base class for this
  LogFile.create(size: download.size, type: download.type, name: url)
end

https://github.com/qoobaa/s3