2011-12-27 127 views
3

我正嘗試使用carrierwave將文件從本地機器上傳到amazon s3。其實我想寫上述操作的遷移。我需要將本地存儲的圖像移動到亞馬遜。任何人都可以告訴我應該如何使用carrierwave的方法來執行上述操作。 順便說一下,我也在carrierwave之上使用Carrierwave_direct,但我不認爲這會影響我的存儲方法。使用carrierwave在rails上將文件上傳到s3從本地機器到s3使用carrierwave在rails中

我執行uploader.store!(/local/path/to/file)但它失敗,出現以下錯誤:

You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.

是否有其他方法可以讓我在方法路徑信息發送?

我也試着執行:

new_file.asset = File.open('full/path') #asset is where my uploader is mounted 

在這種情況下,當我嘗試new_file.save!,它成功保存,但是當我試圖通過幹什麼new_file.asset.url,以便獲得url它顯示爲空。我不知道爲什麼

繼承人我上傳:

module DirectUploader 
    extend ActiveSupport::Concern 

    included do 
    include CarrierWave::MimeTypes 
    include CarrierWave::MiniMagick 

    include CarrierWaveDirect::Uploader 
    include ActiveModel::Conversion 
    extend ActiveModel::Naming 

    process :set_content_type 
    end 

    module InstanceMethods 
    def store_dir 
     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 

    # override the url to return absolute url if available and 
    # revert back to standard functionality if it is not available 
    def url 
     if model.absolute_url.nil? 
     super 
     else 
     model.absolute_url 
     end 
    end 

    def filename 
     @random = Digest::MD5.hexdigest(model.latest_time.to_s) 
     "#{@random}.#{File.extname(original_filename)}" if original_filename 
    end 

    def policy_doc(options={}) 
     options[:expiration] ||= self.class.upload_expiration 
     options[:max_file_size] ||= self.class.max_file_size 

     doc = { 
      'expiration' => Time.now.utc + options[:expiration], 
      'conditions' => [ 
       ["starts-with", "$utf8", ""], 
       ["starts-with", "$authenticity_token", ""], 
       ["starts-with", "$key", store_dir], 
       {"bucket" => fog_directory}, 
       {"acl" => acl}, 
       ["content-length-range", 1, options[:max_file_size]] 
      ] 
     } 
     doc['conditions'] << {"success_action_redirect" => success_action_redirect} if success_action_redirect 
     doc 
    end 

    def policy(options={}) 
     Base64.encode64(policy_doc(options).to_json).gsub("\n","") 
    end 
    end 
end 

並且在carrierwave配置沒有問題,因爲我可以上傳使用表單/ HTML文件。它只是我在遷移期間發現問題。

+0

您可以發佈您配置carrierwave?你是否也需要carrierwave來爲你做任何處理,或者你只是嚴格上傳文件? – aubreyrhodes 2011-12-27 20:03:51

+0

這裏是我使用的上傳文件: – 2011-12-27 20:09:43

回答

0

你試過:

uploader.store!(File.new('/local/path/to/file')) 

當我運行測試,我用:

uploader.store! File.open(Rails.root.join("spec/support/file.png")) 
相關問題