1

与此问题类似的问题,但提供的解决方案并没有解决我的问题。Carrierwave 处理的图像未上传到 S3

使用 Railscast #383 作为代码的基础:http ://railscasts.com/episodes/383-uploading-to-amazon-s3

使用carrierwave_direct 将图像成功上传到S3。我想用sidekiq在后台处理图像,然后上传到S3。

sidekiq worker 完成图像处理没有错误,但处理后的图像(: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

光控制器

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
4

1 回答 1

0

在我的上传器文件夹中,我复制了我的原始上传器并将其重命名为“image_uploader_file.rb”。然后我修改了原始版本“image_uploader.rb”以使用雾和 S3。我假设carrierwave只会使用'image_uploader.rb',但它似乎加载了这两个文件。旧版本使用本地存储,因此这是carrierwave 存储图像的地方。一旦我删除了“image_uploader_file.rb”,处理后的图像就会正确上传到 S3。

于 2013-03-20T15:37:21.533 回答