简体   繁体   中英

Uploading files to s3 from local machine to s3 using carrierwave in rails

I am trying to upload the files from my local machine to amazon s3 using carrierwave. Actually I want to write the migration for the above operations. I need to move the images that are stored locally to amazon. Can anybody tell me how should I perform the above operations using the methods of carrierwave. Btw I am also using Carrierwave_direct on top of carrierwave but I don't think that would affect my storage methods.

I executed uploader.store!(/local/path/to/file) but it fails with the following error:

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

Is there any other way I can send in the path info in the method?

I also tried executing:

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

In this case, when I try new_file.save! , it successfully saves but when I try to get the url by doin new_file.asset.url it shows empty. I don't know why

Heres my uploader:

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

And there is no problem in carrierwave configuration because I can upload the files using form/html. Its just that I am finding problems during migration.

Have you tried:

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

When I'm running tests, I use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM