我已将 Carrierwave+Fog 配置为与 Amazon S3 配合使用。我遇到的问题是,所有内容都是在我的项目的公共(public)文件夹中创建的,并且没有上传到 S3。拇指和目录都正常,只是图像的位置不起作用。
我的上传者:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_fit => [200, 200]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
雾.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'xxx',
:aws_secret_access_key => 'yyy',
:region => 'eu-west-1'
}
config.fog_directory = 'your_bucket_here'
config.fog_public = true
config.fog_attributes = {'Cache-Control' => 'max-age=315576000'}
end
使用它的类:
class Image < ActiveRecord::Base
belongs_to :product
mount_uploader :remote_file, ImageUploader
end
Controller 代码:
i = Image.new
i.save
i.remote_file = params[:image]
render :json => {:response => i.remote_file.url}
响应是: {“响应”:“/uploads/tmp/1387464252-27678-6793/logo11w.png”}
图像是在 Rails 项目的公共(public)目录中创建的。
谢谢!
最佳答案
CarrierWave 使用 ActiveRecord before_save and after_save callbacks 执行上传。在您的 Controller 中,在调用 save
之前设置 remote_file
:
i = Image.new
i.remote_file = params[:image]
i.save
关于ruby-on-rails - Carrierwave 在本地上传图像,而不是到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20684645/