Aws :: S3 :: Errors :: NoSuchKey:指定的密钥不存在

时间:2016-03-04 13:56:55

标签: ruby-on-rails amazon-s3 carrierwave

我正在使用gems carrierwave和carrierwave-aws。 我尝试通过调整S3上的图像来创建一个rake任务

namespace :avatar do
  desc 'Regenerate all user avatars'
  task regenerate_avatars: :environment do
    Bar.find_each do |bar|
      bar.profile_picture.recreate_versions! if bar.profile_picture?
      bar.save!
    end
  end
end

但我收到错误Aws :: S3 :: Errors :: NoSuchKey:指定的密钥不存在。

file carrierwave.rb

CarrierWave.configure do |config|
  config.storage    = :aws
  # for bucket name 
  config.aws_bucket = Rails.application.secrets.aws_bucket_name
  config.aws_acl    = "public-read-write"
  config.aws_authenticated_url_expiration = 60 * 60 * 24 * 7

  config.aws_credentials = {
    access_key_id:     Rails.application.secrets.aws_access_key_id,
    secret_access_key: Rails.application.secrets.aws_secret_access_key,
    region:            Rails.application.secrets.aws_region
  }
end

在文件上传器中

# encoding: utf-8

class BarPictureUploader < CarrierWave::Uploader::Base

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

  # Choose what kind of storage to use for this uploader:
  # Aws storage for images
  storage :aws
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def cache_dir
    Rails.env.test? ? "#{Rails.root}/tmp/spec/uploads/tmp/uploads" : "#{Rails.root}/tmp/uploads"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end
  process :resize_to_fit => [1024, 768] 
  version :bar_detail do
    process :resize_to_fit => [200, 200] 
  end

  version :bar_index do
    process :resize_to_fit => [80, 70] 
  end

  # def recreate_all_versions 
  #   Bar.find_each do |bar|
  #     bar.profile_picture.recreate_versions! if bar.profile_picture?
  #     bar.save!

  #   end


  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
    %w(jpg jpeg gif png JPG JPEG GIF PNG)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

1 个答案:

答案 0 :(得分:0)

namespace :avatar do
  desc 'Regenerate all user avatars'
  task regenerate_avatars: :environment do
    Bar.find_each do |bar|
      begin
        picture = bar.profile_picture
        picture.cache_stored_file!
        picture.retrieve_from_cache!(picture.cache_name)
        picture.recreate_versions!
        bar.save!
      rescue => e
        puts  "ERROR: #{Bar.to_s}: #{bar.id} -> #{e.to_s}"
      end
    end
  end
end