【问题标题】:Carrierwave processed images not uploading to S3Carrierwave 处理的图像未上传到 S3
【发布时间】:2012-06-19 15:06:44
【问题描述】:

我有 Carrierwave 将图像上传到 S3 存储桶。但是,如果我使用 RMagick 处理缩略图,文件只会保存到本地的公共 tmp。注释掉 process 方法会在 S3 上创建原始文件和 thumb 文件(当然 thumb 没有被处理)。不确定为什么在写入本地 tmp 后处理立即停止。代码如下:

class FileUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick
  storage :fog

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

  # Create different versions of your uploaded files:
  version :thumb do
    process :resize_to_fit => [32, 32]
  end
end

导轨 3.2.5 雾 1.3.1 Rmagick 2.13.1 载波 0.6.2 Carrierwave-mongoid 0.2.1

【问题讨论】:

    标签: ruby-on-rails amazon-s3 carrierwave


    【解决方案1】:

    我建议你使用 minimagick:

    class FileUploader < CarrierWave::Uploader::Base
      include CarrierWave::MiniMagick
    end
    

    对于拇指版本,我建议你使用resize_to_fill 方法,例如:

    version :thumb do
       process :resize_to_fill => [32, 32]
       process :convert => :png
     end
    

    您还可以为每张图片使用唯一的令牌:

    def filename
         @name ||= "#{secure_token}.#{file.extension}" if original_filename.present?
       end
    
      protected
      def secure_token
        var = :"@#{mounted_as}_secure_token"
        model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
      end
    

    您必须确保您与您的存储桶的连接在机密文件config/initializers/fog.rb 中是正确的,例如:

    CarrierWave.configure do |config|
      config.fog_credentials = {
        :provider               => 'AWS',
        :aws_access_key_id      => 'your_key',
        :aws_secret_access_key  => 'your_secret_key',
        :region                 => 'us-east-1'
      }
    
      config.fog_directory = 'your_bucket_here'
      config.fog_public = true
      config.fog_attributes = {'Cache-Control' => 'max-age=315576000'} 
    end
    

    【讨论】:

      猜你喜欢
      • 2013-03-07
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 2012-07-08
      • 1970-01-01
      • 2015-09-26
      • 2012-12-06
      相关资源
      最近更新 更多