Heroku文件上传问题

时间:2010-09-11 16:27:30

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

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

我想做的是:

  1. 上传CSV文件
  2. 根据报告来自哪个网络
  3. 去除标题块
  4. 将CSV数据读入数据库。这一步很好。
  5. 控制器代码:

      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上产生错误?

    谢谢!

5 个答案:

答案 0 :(得分:7)

根据Avishal的回答,我在Rails 3中使用它:

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

答案 1 :(得分:4)

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

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

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

答案 2 :(得分:1)

答案 3 :(得分:1)

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

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开始发布“/ projects / 1 / upload_pivotal_csv”for 122.172.25.106 2012-03-23T07:46:00 + 00:00 app [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:在`upload_pivotal_csv' 2012-03-23T07:46:00 + 00:00 app [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”=&gt;“✓”,“authenticity_token”=&gt;“sBmRWpGP3q9Hu7O2cMlmnGTByaTXValxYHw5 + cFoSw0 =”,“file”= &gt;#&gt;,“commit”=&gt;“导入”,“id”=&gt;“1”} 2012-03-23T07:46:00 + 00:00 app [web.1]:在406ms完成

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

答案 4 :(得分:-2)

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