从给定的URL下载文件并立即将其上传到Amazon S3的更直接的方法是什么(+将有关该文件的一些信息保存到数据库中,如名称,大小等)?
现在,我没有使用Paperclip和Carrierwave。
谢谢
答案 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