[英]Rails: Uploading a file to AS3 using fog and resque
問題:
我有一個Rails應用程序,要求用戶上傳某種類型的電子表格(csv,xslx,xsl等)進行處理,這可能是一項昂貴的操作,因此,我們決定將其發送到后台服務作為此問題的解決方案。 我們擔心的問題是,因為我們的生產系統位於Heroku上,所以我們需要先將文件存儲在AS3上,然后再檢索以進行處理。
由於將文件上傳到AS3本身是一項昂貴的操作,因此也可能應將其作為后台作業來完成。 問題在於,由於Resque需要將文件數據放入Redis或稍后進行檢索,因此使用Resque這樣做可能會占用大量RAM。 如您所知,Redis僅將其數據存儲在RAM中,並且更喜歡簡單的鍵值對,因此我們希望嘗試避免這種情況。
以下是一些偽代碼,作為我們想要嘗試的示例:
工人/AS3Uploader.rb
require 'fog'
class AS3Uploader
@queue = :as3_uploader
def self.perform(some, file, data)
# create a connection
connection = Fog::Storage.new({
:provider => 'AWS',
:aws_access_key_id => APP_CONFIG['s3_key'],
:aws_secret_access_key => APP_CONFIG['s3_secret']
})
# First, a place to contain the glorious details
directory = connection.directories.create(
:key => "catalog-#{Time.now.to_i}", # globally unique name
:public => true
)
# list directories
p connection.directories
# upload that catalog
file = directory.files.create(
:key => 'catalog.xml',
:body => File.open(blah), # not sure how to get file data here with out putting it into RAM first using Resque/Redis
:public => true
end
# make a call to Enqueue the processing of the catalog
Resque.enqueue(CatalogProcessor, some, parameters, here)
end
控制器/catalog_upload_controller.rb
def create
# process params
# call Enqueue to start the file processing
# What do I do here? I could send all of the file data here right now
# but like I said previously that means storing potentially 100s of MB into RAM
Resque.enqueue(AS3Uploader, some, parameters, here)
end
我建議你做的方式是
tmp
dir中並獲取file-path
Resque
使用file-path
上傳文件 Resque
將file-path
存儲在redis
而不是整個file-content
(這將非常昂貴) AWS- S3
注意:如果您有多個實例,例如一個實例用於后台處理,一個實例用於數據庫,一個實例作為實用程序實例,則您的tmp目錄可能無法用於其他實例..因此,請將文件存儲在保存有
resque
的實例的temp
目錄中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.