将临时文件从S3托管文件写入Heroku

时间:2015-10-11 22:43:29

标签: ruby-on-rails heroku amazon-s3 temporary-files

我在Heroku上有一个rails应用程序。情况如下:用户应该能够使用s3将PDF(批量实例)上传到我们的应用程序;用户还应该能够获取上传的PDF的s3网址,并使用HyPDF将其拆分为更多PDF,方法是指定文件路径和要拆分的所需页面(创建Essay实例)。

所有这些都发生在与/essays相同的POST请求中。

以下是我今天一直在使用的代码:

 def create
    if params[:essay].class == String 
      batch_id = params[:batch_id].gsub(/[^\d]/, '').to_i
      break_up_batch(params, batch_id)
      redirect_to Batch.find(batch_id), notice: 'Essays were successfully created.'
    else 
      @essay = Essay.new(essay_params)
      respond_to do |format|
        if @essay.save
          format.html { redirect_to @essay, notice: 'Essay was successfully created.' }
          format.json { render :show, status: :created, location: @essay }
        else
          format.html { render :new }
          format.json { render json: @essay.errors, status: :unprocessable_entity }
        end
      end
    end
 end

# this is a private method
def break_up_batch(params, batch_id)
  essay_data = []
  # create a seperate essay for each grouped essay
  local_batch = File.open(Rails.root.join('tmp').to_s + "temppdf.pdf" , 'wb') do |f|
    f.binmode
    f.write HTTParty.get(Batch.find(batch_id).document.url).parsed_response
    f.path
  end

  params["essay"].split("~").each do |data|
    data = data.split(" ")
    hypdf_url = HyPDF.pdfextract(
        local_batch,
        first_page: data[1].to_i, 
        last_page: data[2].to_i,
        bucket: 'essay101',
        public: true

    )
      object = {student_name: data[0], batch_id: batch_id, url: hypdf_url[:url]}
      essay_data << object 
  end

  essay_data.each {|essay| Essay.create(essay)}
  File.delete(local_batch) 
end

我无法让文件显示在Heroku上,我正在检查heroku run bashls tmp。因此,当运行该方法时,会将空白文件上载到S3。我写了一些jQuery来填充一个隐藏的字段,这就是为什么在代码中间有时髦的分裂。

2 个答案:

答案 0 :(得分:1)

由于Heroku的短暂文件系统,我强烈建议尽快从文件系统中删除该文件。也许使用以下内容:

  1. 用户上传到S3(最好是直接:https://devcenter.heroku.com/articles/direct-to-s3-image-uploads-in-rails
  2. 启动后台工作程序以获取文件并在内存中执行必要的处理
  3. 如果在正确处理文件时需要通知用户,请设置&#34; status&#34;数据库中的字段,允许前端应用程序轮询Web服务器以获取更新。显示&#34;处理&#34;用户,直到后台工作人员更改其状态。
  4. 此方法还允许您的Web进程快速响应,而不会占用资源,并可能触发H12(请求超时)错误。

答案 1 :(得分:0)

原来使用File类不是正确的方法。但是使用Tempfile有效!

def break_up_batch(params, batch_id, current_user)
      essay_data = []
      # create a seperate essay for each grouped essay
      tempfile = Tempfile.new(['temppdf', '.pdf'], Rails.root.join('tmp'))
      tempfile.binmode
      tempfile.write HTTParty.get(Batch.find(batch_id).document.url).parsed_response
      tempfile.close
      save_path = tempfile.path

      params["essay"].split("~").each do |data|
        data = data.split(" ")
        hypdf_url = HyPDF.pdfextract(
            save_path,
            first_page: data[1].to_i, 
            last_page: data[2].to_i,
            bucket: 'essay101',
            public: true

        )
          object = {student_name: data[0], batch_id: batch_id, url: hypdf_url[:url]}
          essay_data << object 
      end
      essay_data.each do |essay| 
        saved_essay = Essay.create(essay)
        saved_essay.update_attributes(:company_id => current_user.company_id) if current_user.company_id
      end
      tempfile.unlink
    end