Carrierwave处理的图像未上传到S3

时间:2012-06-17 06:40:09

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

我让Carrierwave上传图像就好了S3桶。但是,如果我使用RMagick处理缩略图,则文件只会在本地保存到公共tmp。注释掉过程方法会在S3上创建原始文件和拇指文件(当然拇指不会被处理)。不确定为什么处理在写入本地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 雾1.3.1 Rmagick 2.13.1 Carrierwave 0.6.2 Carrierwave-mongoid 0.2.1

1 个答案:

答案 0 :(得分: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