首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从S3宿主文件将临时文件写入Heroku

从S3宿主文件将临时文件写入Heroku
EN

Stack Overflow用户
提问于 2015-10-11 22:43:28
回答 2查看 647关注 0票数 1

我有一个rails应用程序托管在Heroku上。情况如下:用户应该能够使用s3将PDF (批处理的实例)上传到我们的应用程序中;用户还应该能够获取上传的PDF的s3 web地址,并通过指定文件路径和要分割的页面(创建文章实例)将其分割成更多的PDF。

所有这些都发生在对/essays的同一个POST请求中。

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

代码语言:javascript
复制
 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来填充一个隐藏字段,这就是为什么代码中间会出现奇怪的分裂。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-31 20:29:43

事实证明,使用File类并不是正确的方法。但是使用Tempfile是有效的!

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2015-10-15 17:36:53

由于Heroku的临时文件系统,我强烈建议尽快将该文件从您的文件系统中删除。也许可以使用以下方法:

  1. 用户上传到S3 (最好是直接:https://devcenter.heroku.com/articles/direct-to-s3-image-uploads-in-rails)
  2. 启动后台工作人员,以获取文件并在内存中进行必要的处理。
  3. 如果需要在正确处理文件时通知用户,请在DB中设置一个"status“字段,并允许前端应用程序轮询web服务器进行更新。向用户显示“处理”,直到后台工作人员更改其状态为止。

此方法还允许您的web进程快速响应,而不需要占用资源,并且可能触发H12 (请求超时)错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33070898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档