2012-02-03 42 views
5

我試圖爲我的應用程序開發直接的文件上傳到S3。我正在關注這個github教程,一切都或多或少都可以,但在嘗試進行後期處理時會收到錯誤消息。save_and_process後處理403禁止的Carrierwave_direct S3霧

我做了以下內容:

我有一個名爲clip.rb ActiveRecord模型:

class Clip < ActiveRecord::Base 
    belongs_to :attachable, :polymorphic => true 
    mount_uploader :avatar, AvatarUploader 

    attr_accessible :id, :avatar, :name, :clipat_file_name, :attachable_id, :attachable_type, :clipat, :project_id, :user_id, :path, :parent_id, 

    def save_and_process_avatar(options = {}) 
    if options[:now] or 1==1 
     self.remote_avatar_url = avatar.direct_fog_url(:with_path => true) 
     save 
    else 
     Resque.enqueue(AvatarProcessor, attributes) 
    end 
    end 

然後,我有一個上傳:avatar_uploader.rb

class AvatarUploader < CarrierWave::Uploader::Base 
    include CarrierWave::RMagick 
    include CarrierWaveDirect::Uploader 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}" #I removed /#{model.id} from the template because it creates an empty directory on the server. But If I put it back, the same problem remains 
    end 
    version :thumb do 
    process :resize_to_limit => [50, 50] 
    end 
end 

和化身控制器:

class AvatarsController < ApplicationController 
    def new 
    @uploader = Clip.new.avatar 
    @uploader.success_action_redirect = 'http://localhost:3000/clips' 
    end 
end 

最後我clip_controller:

class ClipsController < ApplicationController 
    def index 
    if params[:key] 
     key=params[:key].split("/") 
     clip = Clip.new 
     clip.attachable_id = key[3] 
     clip.attachable_type = "Pmdocument" 
     clip.key = params[:key] 
#  clip.save 
     clip.save_and_process_avatar 
    end 
    @clips = Clip.where("avatar is not null") 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @clips.collect { |p| p.to_jq_upload }.to_json } 
    end 
    end 

當我上傳文件,如果我只是救我的「夾子」,一切正常。然而如果我使用save_and_process方法,錯誤發生在線路: self.remote_avatar_url = avatar.direct_fog_url(:with_path =>真)

這是錯誤消息:

OpenURI::HTTPError (403 Forbidden): 
    app/models/clip.rb:38:in `save_and_process_avatar' 
    app/controllers/clips_controller.rb:22:in `index' 

Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms) 
Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms) 
Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (5.2ms) 

我已經掛在這兩天所以,任何幫助將不勝感激!謝謝!!!尼古拉斯。

+0

這條線的結果是什麼? 'self.remote_avatar_url = avatar.direct_fog_url(:with_path => true)' – pschuegr 2012-12-13 23:28:15

+0

我已經停止使用Carrierwave很長時間了(回到Paperclip)。所以我不能回答你的問題。抱歉... – ndemoreau 2012-12-14 09:48:48

回答

2

我敢打賭,提供給self.remote_avatar_url的URL不正確。我遇到了同樣的問題,CWDirect gem提供的代碼並不適用於我,並且給了我一個不正確的URL,因此CarrierWave無法下載和處理圖像。整個403 Forbidden錯誤信息是亞馬遜的垃圾信息 - 這導致人們相信權限有問題;在我的情況下,權限絕對沒有錯。那只是因爲那裏沒有形象。下面是得到了這個工作對我的代碼,請注意我已經改變了URL是如何形成的:

def save_and_process_image(options = {}) 
if options[:now] 
    # debugger 
    self.remote_image_url = image.direct_fog_url+self.key # OLD CODE THAT AINT WORKIN! --> image.direct_fog_url(:with_path => true) 
    save 
else 
    # Resque.enqueue(AvatarProcessor, attributes) 
    # TODO: Implement background processing 
end 
end 

注意,我安裝字段的名稱是image而不是avatar

我如何得到這一點並修復它 - 試試看,使用rails調試器(只取消上面的調試器行註釋)在self.remote_image_url行之前凍結程序,然後在調試模式下輸入irb啓動控制檯。然後你可以打印出來,看看'image.direct_fog_url(:with_path => true)'給你的值是多少。您可以將其複製並粘貼到瀏覽器中。如果它是錯誤的(可能是),那麼你會得到愚蠢的權限錯誤(即使它不是權限問題),但是當它正確時,你會看到上傳的圖像或圖像將下載。

打開您的Amazon S3控制檯並查看您的開發存儲桶以便您可以找到剛剛上傳的圖像是個好主意。在控制檯中查找圖像並轉到其屬性,您可以看到您使用的網址應該是

希望這會有所幫助。由於誤導性的錯誤,我很難追蹤到,我花了一些時間來嘗試糾正S3存儲桶的權限,但這不是問題,只是來自CWDirect github頁面的代碼不適用於我(寶石版)。