ruby-on-rails-3 - Carrierwave 直接上传到 s3,sidekiq 验证失败

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

遵循 Ryan Bates 教程,使用 CarrierWave 直接上传到 S3,并使用 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 配置

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

拍摄 Controller 显示负责渲染上传表单的操作

  # 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

展示操作上传者表单

<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 -->

最佳答案

原来我的存储桶名称无效...请确保您的存储桶名称仅包含字母、数字和破折号。

关于ruby-on-rails-3 - Carrierwave 直接上传到 s3,sidekiq 验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13323061/

相关文章:

ruby-on-rails - 如何在 Rails (3) 中用回形针裁剪特定区域?

ruby - 不断收到 @controller is nil 错误

java - 如何在 Servlet 请求中将传入字节直接写入磁盘?

php - 点击后如何获取图片id?

amazon-s3 - 查询寻址的 S3 存储桶与该集群位于不同的区域

ruby-on-rails - 按照 RoR 约定从类名中检索 Controller 名称

mysql - 在 Rails 中的 SQL 查询期间去除电话号码中的非数字值

java - 是否可以通过上传/通过上传的文件来攻击服务器?

html - HTML/CSS客户门户

python - 无法在 AWS Lambda 函数中导入 sqlalchemy