【问题标题】:Heroku file upload problemHeroku文件上传问题
【发布时间】:2011-04-11 03:25:12
【问题描述】:

我在将 CSV 文件上传到 Heroku 并对其进行处理时遇到问题。它在我的本地环境中运行良好。请注意,我不需要将文件保存在 Heroku 上,只需在请求期间访问它即可将其转换为字符串以进行处理并导入数据库。

我想做的是:

  1. 上传 CSV 文件
  2. 根据报告来自哪个网络删除标题块
  3. 将 CSV 数据读入数据库。这一步工作正常。

控制器代码:

  def create
    @account = Account.find(params[:report][:account_id])
    @file = params[:report][:file].read
    # logger.info file.inspect
    case @account.provider
    when "Microsoft AdCenter" then @file.gsub!(/\A(.*)\n\n/im, "")
    when "Google AdWords" then @file.gsub!(/\A(.*)\n/i, "")
    else
      raise "Invalid PPC report format"
    end
  end

这是堆栈跟踪:

Processing ImportController#create (for XX.182.6.XXX at 2010-09-11 09:19:01) [POST]
  Parameters: {"commit"=>"Upload", "action"=>"create", "authenticity_token"=>"XXXXXwoFpvRO3vN8XVXRDg8rikFsj2TFTW7mrcTgg=", "controller"=>"import", "report"=>{"account_id"=>"1", "file"=>#<File:/home/slugs/126077_0657264_9a92/mnt/tmp/RackMultipart.9845.0>}}

NoMethodError (private method `gsub!' called for #<Tempfile:0x2b8ccb63ece0>):
  /usr/local/lib/ruby/1.8/delegate.rb:270:in `method_missing'
  app/controllers/import_controller.rb:15:in `create'
  warden (0.10.7) lib/warden/manager.rb:35:in `call'
  warden (0.10.7) lib/warden/manager.rb:34:in `catch'
  warden (0.10.7) lib/warden/manager.rb:34:in `call'
  /home/heroku_rack/lib/static_assets.rb:9:in `call'
  /home/heroku_rack/lib/last_access.rb:25:in `call'
  /home/heroku_rack/lib/date_header.rb:14:in `call'
  thin (1.0.1) lib/thin/connection.rb:80:in `pre_process'
  thin (1.0.1) lib/thin/connection.rb:78:in `catch'
  thin (1.0.1) lib/thin/connection.rb:78:in `pre_process'
  thin (1.0.1) lib/thin/connection.rb:57:in `process'
  thin (1.0.1) lib/thin/connection.rb:42:in `receive_data'
  eventmachine (0.12.6) lib/eventmachine.rb:240:in `run_machine'
  eventmachine (0.12.6) lib/eventmachine.rb:240:in `run'
  thin (1.0.1) lib/thin/backends/base.rb:57:in `start'
  thin (1.0.1) lib/thin/server.rb:150:in `start'
  thin (1.0.1) lib/thin/controllers/controller.rb:80:in `start'
  thin (1.0.1) lib/thin/runner.rb:173:in `send'
  thin (1.0.1) lib/thin/runner.rb:173:in `run_command'
  thin (1.0.1) lib/thin/runner.rb:139:in `run!'
  thin (1.0.1) bin/thin:6
  /usr/local/bin/thin:20:in `load'
  /usr/local/bin/thin:20

Rendering /disk1/home/slugs/126077_0657264_9a92/mnt/public/500.html (500 Internal Server Error)

任何人都知道为什么它在本地工作得很好,但在 Heroku 上却产生了这个错误?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby file-upload heroku fastercsv


    【解决方案1】:

    根据 Avishal 的回答,我将它与 Rails 3 一起使用:

    @file = IO.read(params[:report].tempfile.path)
    

    【讨论】:

      【解决方案2】:

      实际上并不完全正确。事实证明,如果您将文件上传到 Heroku,您可以在请求期间通过 Tempfile 类访问它。我能够将临时文件读入字符串,然后根据需要对其进行处理(它是 CSV):

      @file = IO.read(params[:report][:file].path)
      

      适用于文本/csv 文件,但我想如果你想做的不仅仅是基本的文本操作,你必须按照 Joost 的建议使用 S3。

      【讨论】:

      • 请您详细说明一下,因为我正在寻找相同的东西。
      • 我能够以一种迂回的方式读取和解析 CSV 文件,但这是我在 import_controller.rb 中输入的内容。 @file = IO.read(params[:report][:file].path); FasterCSV.parse(@file, :headers =&gt; true, :skip_blanks =&gt; true) do |row| ... end;
      【解决方案3】:
      【解决方案4】:

      我仍然对这个问题感到厌烦。我尝试了这里给出的解决方案,以便能够上传 csv 文件,然后解析它以通过活动记录填充我的数据库。我有以下代码:

      file = IO.read(params[:file].tempfile.path)
          FasterCSV.new(file, :headers => true).each do |row|
               # my parsing logic
          end
      

      该代码在本地运行良好,但在 Heruko 中根本无法运行。我收到错误,我在 heruko 日志中看到的只是:

      在 2012 年 3 月 23 日 07:45:59 +0000 开始为 122.172.25.106 发布 POST "/projects/1/upload_pivotal_csv" 2012-03-23T07:46:00+00:00 应用程序 [web.1]: 2012-03-23T07:46:00+00:00 app[web.1]: NotImplementedError(请切换到 Ruby 1.9 的标准 CSV 库。它是 FasterCSV 加上对 Ruby 1.9 的 m17n 编码引擎的支持。) : 2012-03-23T07:46:00+00:00 app[web.1]: app/controllers/projects_controller.rb:17:in `upload_pivotal_csv' 2012-03-23T07:46:00+00:00 应用程序 [web.1]: 2012-03-23T07:46:00+00:00 应用程序 [web.1]: 2012-03-23T07:46:00+00:00 heroku[路由器]: POST castletrack.herokuapp.com/projects/1/upload_pivotal_csv dyno=web.1 queue=0 wait=0ms service=783ms status=500 bytes=728 2012-03-23T07:46:00+00:00 app[web.1]:由 ProjectsController#upload_pivotal_csv 处理为 HTML 2012-03-23T07:46:00+00:00 app[web.1]: 参数: {"utf8"=>"✓", "authenticity_token"=>"sBmRWpGP3q9Hu7O2cMlmnGTByaTXValxYHw5+cFoSw0=", "file"=># >, "提交"=>"导入", "id"=>"1"} 2012-03-23T07:46:00+00:00 app[web.1]:在 406 毫秒内完成

      我确信这与 ruby​​ 版本有关。我在本地使用 ruby​​ 1.8.7 及其 rails 3 应用程序。我不确定heruko上有什么红宝石版本。但我可以看出我正在使用 ceder 堆栈。

      【讨论】:

      • 您对 Ruby 1.8.7/1.9 问题的看法是正确的。通常,您需要确保本地和部署的 Ruby 版本相同。在 Ruby 1.9 中,标准 CSV 库是 FasterCSV - 这就是错误消息试图告诉您的内容:“切换到 Ruby 1.9 的标准 CSV 库”
      【解决方案5】:

      是的。文件系统是只读的。你可以store your files on S3

      【讨论】:

      • 文件系统不是只读的。它不是从一个请求到下一个请求的持久性。
      猜你喜欢
      • 2017-09-06
      • 1970-01-01
      • 2010-10-17
      相关资源
      最近更新 更多