3
什麼是從給定的URL下載文件並立即上傳到Amazon S3的更直接的方式(+保存到數據庫中的一些關於文件的信息,如名稱,大小等)?Rails:從URL保存文件並將其保存到Amazon S3
現在,我沒有使用Paperclip既無Carrierwave。
謝謝
什麼是從給定的URL下載文件並立即上傳到Amazon S3的更直接的方式(+保存到數據庫中的一些關於文件的信息,如名稱,大小等)?Rails:從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
你在哪裏寫的嗎? – 2ueenO 2013-12-10 15:46:03
在一個純文本文件中,然後用'$ ruby file.rb'(?) – nicooga 2013-12-10 17:52:15
運行它,但是我的解決方案非常有趣,我想將它插入我的網站以存儲來自鳥舍的圖片。我創建了另一個問題:[在S3中從一個臨時URL保存圖片](http://stackoverflow.com/questions/20500057/save-a-picture-in-s3-from-a-temporary-url) – 2ueenO 2013-12-10 18:09:02