使用carrierwave在rails上将文件上传到s3从本地机器到s3使用carrierwave在rails中

问题描述:

我正尝试使用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

你试过:

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

当我运行测试,我用:

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