5

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

现在,我既没有使用 Paperclip,也没有使用 Carrierwave。

谢谢

4

1 回答 1

9

直截了当:

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

于 2013-03-14T15:33:11.667 回答