繁体   English   中英

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

[英]Rails: save file from URL and save it to Amazon S3

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

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

谢谢

直截了当:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM