2012-06-17 113 views
0

我有Carrierwave上傳圖像很好,S3存儲桶。但是,如果我使用RMagick處理縮略圖,則只能將文件保存到本地的公共tmp。註釋處理方法在S3上創建原始文件和縮略圖文件(當然,不處理該縮略圖)。不知道爲什麼處理在寫入本地tmp後立即停止。下面的代碼:Carrierwave處理的圖像沒有上傳到S3

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

回答

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 
+0

完美,謝謝您。 – realdeal

+0

不客氣! – hyperrjas

相關問題