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

use*_*621 5 ruby upload ruby-on-rails download amazon-s3

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

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

谢谢

nic*_*oga 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
Run Code Online (Sandbox Code Playgroud)

https://github.com/qoobaa/s3