Carrierwave通过sidekiq无法验证直接上传到s3

时间:2012-11-10 14:49:52

标签: ruby-on-rails-3 upload amazon-s3 carrierwave sidekiq

关于使用CarrierWave直接上传到S3的Ryan Bates教程以及使用Sidekiq的后台作业。

我在sidekiq日志中看到后台工作程序中出现以下错误。

    2012-11-10T14:33:56Z 44401 TID-owd2xrzdg WARN: {"retry"=>true, "queue"=>"default", "class"=>"Article::ImageWorker", "args"=>[44, "uploads/d8850e90-0d6d-0130-fecf-3c0754129341/mkv5E.jpg"], "jid"=>"8add5079541725ad30550f9c", "error_message"=>"Validation failed: Image could not download file", "error_class"=>"ActiveRecord::RecordInvalid", "failed_at"=>"2012-11-10T14:29:15Z", "retry_count"=>4, "retried_at"=>2012-11-10 14:33:56 UTC}
    2012-11-10T14:33:56Z 44401 TID-owd2xrzdg WARN: Validation failed: Image could not download file
    2012-11-10T14:33:56Z 44401 TID-owd2xrzdg WARN: /Users/kgoddard/.rvm/gems/ruby-1.9.3-head/gems/activerecord-3.2.8/lib/active_record/validations.rb:56:in `save!'

这是我的上传者

class ImageUploader < CarrierWave::Uploader::Base
  # Include CarrierWave direct uploader to background upload task.
  include CarrierWaveDirect::Uploader

  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  # Choose what kind of storage to use for this uploader:
  # storage :file
  storage :fog

  # Set the mimetype of the upload incase it is incorrect.
  include CarrierWave::MimeTypes
  process :set_content_type

  process :resize_to_limit => [640, 640]

  version :thumb do
    process :resize_to_fill => [160, 120]
  end

end

文章模型

class Article < ActiveRecord::Base
  attr_accessible :orientation, :shoot_id, :image
  belongs_to :shoot
  mount_uploader :image, ImageUploader

  after_save :enqueue_image

  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

    def perform(id, key)
        article = Article.find(id)
        article.key = key
        article.remote_image_url = article.image.direct_fog_url(:with_path => true)
        article.save!
        article.update_column(:image_processed, true)
    end

  end

end

CarrierWave config

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',       # required
    :aws_access_key_id      => ENV["AWS_ACCESS_KEY_ID"],       # required
    :aws_secret_access_key  => ENV["AWS_SECRET_ACCESS_KEY"],       # required
    :region                 => 'eu-east-1'  # optional, defaults to 'us-east-1'
  }
  config.fog_directory  = ENV["AWS_S3_BUCKET"]                       # required
end

拍摄控制器显示负责渲染上传表单的操作

  # GET /shoots/1
  # GET /shoots/1.json
  def show
    # TODO: is this the best way to prevent users from accessing not owned resources.
    @shoot = Shoot.find(params[:id])
    @uploader = Article.new.image
    @uploader.success_action_redirect = new_admin_article_url    
    if @shoot
      respond_to do |format|
        format.html # show.html.erb
      end
    else
      redirect_to shoots_path, alert: 'No shoot found with id: #{params[:id]}'
    end
  end

show action uploader的表单

<div class="hero-unit">
  <h2><%= @shoot.name %></h2>
  <p><%= @shoot.overview %></p>
  <p>This shoot was taken on <%= @shoot.shoot_date %></p>
  <%= direct_upload_form_for @uploader do |f| %>
    <p><%= f.file_field :image, multiple: true, name: "article[image]" %></p>
    <p><%= f.submit "Add Media", :class => "btn btn-primary" %></p>
  <% end %>
</div><!-- /hero-unit -->

1 个答案:

答案 0 :(得分:3)

原来我的存储桶名称无效...请确保您的存储桶名称只包含字母,数字和短划线。