载波处理的图像未上传到AWS S3 [英] Carrierwave processed images not uploading to AWS S3
问题描述
与此问题类似的问题,但是提供的解决方案无法解决我的问题. 载波处理后的图像未上传到S3
Similar problem to this question but the solution provided did not fix my problem. Carrierwave processed images not uploading to S3
使用Railscast#383作为代码的基础: http://railscasts. com/episodes/383-uploading-to-amazon-s3
Using Railscast #383 as basis for code: http://railscasts.com/episodes/383-uploading-to-amazon-s3
已使用carrierwave_direct将图像成功上传到S3.我想使用sidekiq在后台处理图像,然后将其上传到S3.
The image is successfully uploaded to S3 using carrierwave_direct. I want to process the images in the background with sidekiq and then upload to S3.
sidekiq worker完成了图像处理,没有错误,但是处理过的图像(:thumb和:large)本地存储在public/uploads文件夹中.
The sidekiq worker completes the image processing without error but the processed images (:thumb and :large) are stored locally in the public/uploads folder.
您知道为什么未将处理后的图像上传到S3吗?
Any idea why the processed images are not being uploaded to 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
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 %>
initializers/carrierwave.rb
initializers/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 *根据对上面链接的问题的回答添加了它
initializers/fog.rb *added this based on the response to the question linked above
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
推荐答案
在我的上载器文件夹中,我复制了原始上载器,并将其重命名为"image_uploader_file.rb".然后,我修改了原始版本"image_uploader.rb"以使用fog和S3.我以为载波只使用"image_uploader.rb",但看起来它加载了两个文件.旧版本使用本地存储,因此是载波存储图像的位置.一旦我删除了"image_uploader_file.rb",处理后的图像就会正确上传到S3.
In my uploaders folder I had copied my original uploader and renamed it 'image_uploader_file.rb'. I then modified the origial version 'image_uploader.rb' to use fog and S3. I assumed that carrierwave would only use 'image_uploader.rb' but it appears that it loaded both files. The old version used local storage so that is where carrierwave stored the images. Once I deleted 'image_uploader_file.rb' the processed images were properly uploaded to S3.
这篇关于载波处理的图像未上传到AWS S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!