此问题的类似问题,但提供的解决方案并没有解决我的问题。 Carrierwave processed images not uploading to S3
使用Railscast#383作为代码的基础:http://railscasts.com/episodes/383-uploading-to-amazon-s3
使用carrierwave_direct将图像成功上传到S3。我想用sidekiq在后台处理图像,然后上传到S3。
sidekiq工作者完成图像处理而没有错误,但处理过的图像(:thumb和:large)本地存储在public / uploads文件夹中。
知道为什么处理后的图像没有上传到S3?
者:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
include CarrierWave::MimeTypes
process :set_content_type
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
version :thumb do
process :resize_to_limit => [200, 200]
end
version :large do
process :resize_to_limit => [600, 400]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
型号:
class Photo < ActiveRecord::Base
attr_accessible :name, :image, :remote_image_url, :user_id
mount_uploader :image, ImageUploader
belongs_to :user
after_commit :enqueue_image, :on => :create
has_many :comments, as: :commentable
def image_name
File.basename(image.path || image.filename) if image
end
def enqueue_image
ImageWorker.perform_async(id, key) if key.present?
end
class ImageWorker
include Sidekiq::Worker
sidekiq_options retry: false
def perform(id, key)
photo = Photo.find(id)
photo.key = key
photo.remote_image_url = photo.image.direct_fog_url(with_path: true)
photo.save!
end
end
end
PhotoController
class PhotosController < ApplicationController
def index
@photos = Photo.all
@uploader = Photo.new.image
@uploader.success_action_redirect = new_photo_url
end
查看
.
.
.
<%= direct_upload_form_for @uploader do |f| %>
<p><%= f.file_field :image %></p>
<p><%= f.submit "Upload Photo" %></p>
<% end %>
初始化/ carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],
aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"]
}
config.fog_directory = ENV["AWS_S3_BUCKET"]
end
initializers / fog.rb *根据对上面链接的问题的回复添加了这个
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => ENV["AWS_ACCESS_KEY_ID"],
:aws_secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"],
:region => 'us-east-1'
}
config.fog_directory = ENV["AWS_S3_BUCKET"]
config.fog_public = true
config.fog_attributes = {'Cache-Control' => 'max-age=315576000'}
end
答案 0 :(得分:0)
在我的上传器文件夹中,我复制了我的原始上传器并将其重命名为'image_uploader_file.rb'。然后我修改了原始版本'image_uploader.rb'以使用雾和S3。我假设carrierwave只会使用'image_uploader.rb',但它似乎加载了两个文件。旧版本使用本地存储,因此载波存储图像。删除'image_uploader_file.rb'后,已处理的图像已正确上传到S3。