2011-12-27 10 views
3

キャリアウェーブを使用してローカルマシンからamazon s3にファイルをアップロードしようとしています。実際には、上記の操作のための移行を記述したいと思います。私はアマゾンにローカルに保存されている画像を移動する必要があります。誰も私がどのように上記の操作をcarrierwaveの方法を使って実行すべきか教えてください。 Btw私はまた、搬送波の上にCarrierwave_directを使用していますが、私の記憶方法には影響しないとは思いません。キャリアウェーブを使用してローカルマシンからs3にファイルをアップロードする

私は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 doinのことで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 

そして、私は、フォーム/ HTMLを使用してファイルをアップロードすることができますので、carrierwaveの構成に問題がない:私はHERESに私のアップローダーを理由

を知りません。それは私が移行中に問題を見つけることだけです。私がテストを実行している場合は

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

は、私が使用:

uploader.store! File.open(Rails.root.join("spec/support/file.png")) 
+0

あなたはcarrierwaveのためのコンフィギュレーションを投稿することができます – aubreyrhodes

+0

ここに私が使用しているアップローダーファイル: –

答えて

0

は、あなたがしようとしたことがありますか?また、あなたのための処理を行うためにcarrierwaveが必要ですか、それともファイルを厳密にアップロードしていますか?
関連する問題