2010-09-11 127 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