301 S3上传后永久移动(301 Moved Permanently after S3 uploa

2019-07-04 11:12发布

我试着去使用carrierwave和雾宝石on Rails的上载红宝石图像S3,图像正确上传但是当我尝试TU节省约含刚上传即时得到这个错误图像信息的模型:

Excon::Errors::MovedPermanently in UserController#show
app/models/user.rb:46:in `process_image_with_key'
app/controllers/user_controller.rb:12:in `show'

<Excon::Response:0x007f97846a3c18 @body="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>PermanentRedirect</Code><Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message>

用户模式:

mount_uploader :image, AvatarUploader

def image_name
  File.basename(image.path || image.filename) if image
end

def process_image_with_key( key )
  unless key.nil?
    self.key = key
    self.remote_image_url = self.image.direct_fog_url(with_path: true)
    self.save!
  end
end

AvatarUploader:

# encoding: utf-8

class AvatarUploader < CarrierWave::Uploader::Base

  include CarrierWaveDirect::Uploader

  include CarrierWave::RMagick

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

  include CarrierWave::MimeTypes
  process :set_content_type

  version :thumb do
    process resize_to_fill: [50, 50]
  end

end

用户控制器

def show
  @user = User.find_by_id(params[:id])
  @user.process_image_with_key(params[:key])
  @uploader = User.new.image
  @uploader.success_action_redirect = user_url(@user.id)
end

carriwerwave初始化

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-west-1'
  }
  config.fog_directory  = ENV['AWS_FILE_BUCKET']
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end

的Gemfile

gem 'carrierwave'
gem 'rmagick'
gem 'fog'
gem 'carrierwave_direct'

Answer 1:

<Error><Code>PermanentRedirect</Code><Message>The bucket you are attempting to access
must be addressed using the specified endpoint. Please send all future requests to 
this endpoint.</Message></Error>

这是一个经常遇到的问题:您正在尝试在区域美西-1访问斗,然而,由于遗留原因默认Amazon S3的区域在大多数/所有AWS的SDK是美国标准 ,它会自动将请求路由到设施北弗吉尼亚州或使用网络图 (见西北太平洋 地区和端点的详细信息)。

因此,您只需要明确的使用S3 API,例如,对于美国西1号之前,指定桶区域的终点:

  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-west-1'
    :endpoint               => 'https://s3-us-west-1.amazonaws.com/'
  }


Answer 2:

再次感谢斯蒂芬欧宝 !

但是,一些考虑我没有做,我区是美国标准,因此,我carrierwave初始化看起来是这样的:#:区域=>#不需要用美国标准:终点=>“ https://s3.amazonaws.com ”

这个环节是关键:d



文章来源: 301 Moved Permanently after S3 uploading