3

我在将 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 上产生了该错误?

谢谢!

4

5 回答 5

7

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

@file = IO.read(params[:report].tempfile.path)
于 2011-01-06T06:35:27.063 回答
4

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

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

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

于 2010-09-28T13:35:16.110 回答
1

你知道吗?

http://devcenter.heroku.com/categories/platform-constraints

于 2010-09-11T22:41:09.247 回答
1

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

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

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

2012-03-23 07:45:59 +0000 2012-03-23T07:46:00+00:00 app[web.1]: 2012-03开始​​ POST "/projects/1/upload_pivotal_csv" for 122.172.25.106 -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 应用程序 [web.1]: 应用程序/控制器/projects_controller.rb:17:in `upload_pivotal_csv' 2012-03-23T07:46:00+00:00 应用程序 [web .1]: 2012-03-23T07:46:00+00:00 app[web.1]: 2012-03-23T07:46:00+00:00 heroku[router]: 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"=> #>, "commit"=>"Import", "id"=>"1"} 2012-03-23T07:46:00+00:00 app[web.1]: 406ms 完成

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

于 2012-03-23T07:54:37.787 回答
-2

是的。文件系统是只读的。您可以将文件存储在 S3 上

于 2010-09-28T05:27:50.707 回答