【问题标题】:Rails: Uploading a file to AS3 using fog and resqueRails:使用雾和 resque 将文件上传到 AS3
【发布时间】:2015-11-26 01:33:04
【问题描述】:

问题:

我有一个 Rails 应用程序,它要求用户上传某种类型的电子表格(csv、xslx、xsl 等)进行处理,这可能是一项代价高昂的操作,因此我们决定将其发送到后台服务作为解决方案到这个问题。我们担心的问题是,因为我们的生产系统在 Heroku 上,所以我们需要先将文件存储在 AS3 上,然后再检索以进行处理。

因为将文件上传到 AS3 本身就是一项成本高昂的操作,所以这可能也应该作为后台作业来完成。问题在于使用 Resque 执行此操作可能会占用大量 RAM,因为 Resque 需要将文件数据放入 Redis 或稍后检索。如您所知,Redis 仅将其数据存储在 RAM 中,并且更喜欢简单的键值对,因此我们希望尽量避免这种情况。

这里有一些伪代码作为我们想要尝试和做的示例:

workers/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

【问题讨论】:

    标签: ruby-on-rails heroku redis resque fog


    【解决方案1】:

    我建议你这样做的方式是

    • 将您的文件存储在您创建的tmp 目录中并获取file-path
    • 告诉Resque使用file-path上传文件
    • 使Resque 将file-path 存储在redis 而不是整个file-content 中(这将非常昂贵)
    • 现在工作人员会将文件上传到AWS- S3

    注意:如果您有多个实例,例如一个用于后台处理的实例,一个用于数据库,一个作为实用程序实例,那么您的 tmp 目录可能对其他实例不可用。所以将文件存储在 temp 目录中持有resque的实例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 2012-03-16
      • 2014-07-13
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      相关资源
      最近更新 更多