0

我有 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

Rails 3.2.5 Fog 1.3.1 Rmagick 2.13.1 Carrierwave 0.6.2 Carrierwave-mongoid 0.2.1

4

1 回答 1

0

我建议你使用 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
于 2012-06-17T11:37:13.167 回答