2010-09-11 223 views
3

我一直在上传CSV文件到Heroku并进行处理时出现问题。它在我的本地环境中工作正常。只需要清楚,我不需要将文件保存在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上的错误?

谢谢!

回答

7

基于Avishal的答案,我用这个了Rails 3:

@file = IO.read(PARAMS [:报告] .tempfile.path)

-2

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

+0

文件系统不是只读的。从一个请求到另一个请求并不是一成不变的。 – Steve 2014-11-24 15:02:18

4

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

文本/ CSV文件
@file = IO.read(params[:report][:file].path) 

工作正常,但我想,如果你想要做的比基本的文字处理更多你必须像Joost所说的那样和S3一起去。

+0

请你详细说明因为我寻找相同的东西。 – 2010-12-13 18:05:06

+1

我能够以一种迂回的方式读取和解析CSV文件,但这是我放入我的'import_controller.rb'中的。 '@file = IO.read(params [:report] [:file] .path); FasterCSV.parse(@file,:headers => true,:skip_blanks => true)do | row | ... end;' – Avishai 2010-12-15 18:19:18

1

我仍然有这个问题闹心。我试着在这里给出的解决方案能够做一个CSV文件上传,然后解析它来填补我的数据库通过活动记录。我有以下代码:

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

该代码只在当地完美工作,但在Heruko中完全不起作用。我得到的错误和所有我能看到的在heruko日志中是这样的:

开始POST“/ projects/1/upload_pivotal_csv”为122.172.25.106于2012-03-23 07:45:59 +0000 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: in`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字节= 728 2012-03-23T07:46:00 + 00:00应用[web.1]:通过ProjectsController处理#upload_pivotal_csv作为HTML 2012-03-23T07:46:00 + 00:00 app [web。 1]:参数{“utf8”=>“✓”,“authenticity_token”=>“sBmRWpGP3q9Hu7O2cMlmnGTByaTXValxYHw5 + cFoSw0 =”,“file”=>#>,“commit”=>“导入”,“id”=> 1“} 2012-03-23T07:46:00 + 00:00 app [web.1]:完成于406ms

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

+0

你对Ruby 1.8.7/1.9的问题是正确的。通常你会想确保你的本地和部署的Ruby版本是相同的。在Ruby 1.9中,标准的CSV库是FasterCSV--这就是错误信息试图告诉你的: “切换到Ruby 1.9的标准CSV库” – Steve 2014-11-24 15:07:52