【问题标题】:301 Moved Permanently after S3 uploading301 在 S3 上传后永久移动
【发布时间】:2012-11-21 10:16:41
【问题描述】:

我尝试使用carrierwave和fog gems将图像上传到Ruby on Rails上的S3,图像上传正确但是当我尝试保存包含有关刚刚上传的图像信息的模型时,我收到此错误:

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

头像上传者:

# 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

载波初始化器

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

宝石文件

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

【问题讨论】:

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


    【解决方案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>
    

    这是一个经常遇到的问题:您正在尝试访问区域 us-west-1 中的存储桶,但是,由于遗留原因,大多数/所有 AWS SDKs 区域中的默认 Amazon S3美国标准使用网络地图自动将请求路由到弗吉尼亚北部或太平洋西北部的设施(有关详细信息,请参阅Regions and Endpoints)。

    因此,您只需在使用 S3 API 之前明确指定存储桶区域的端点,例如us-west-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/'
      }
    

    【讨论】:

    • 谢谢!有用!但有些考虑我还没有考虑,下面我会解释...
    • 顺便说一句,我在从 facebook/twitter 用户上传远程图像时收到一条奇怪的验证消息,我不知道你是否有几分钟的时间观看它StackOverflow Question跨度>
    • 这让我得到了答案——虽然很有趣——我在 aws 控制台中存储桶的 url 中的区域与属性下列出的区域不同——所以明智的话——仔细检查一下。它为 moi 解决了这个问题。
    • 对我来说,这个最简单的答案也很有效:stackoverflow.com/questions/10630430/…
    • +1 @Jackson_Sandland,我遇到了完全相同的情况——真的令人眼花缭乱,因为对于 S3,控制台的 Regions 下拉菜单说它们无关紧要......但它们确实适用于上传!
    【解决方案2】:

    再次感谢Steffen Opel

    但是我没有考虑到,我的地区是美国标准,因此,我的载波初始化程序如下所示: # :region => # 美国标准不需要 :endpoint => 'https://s3.amazonaws.com'

    这个link 是关键:D

    【讨论】:

      猜你喜欢
      • 2015-07-10
      • 2019-11-04
      • 2012-07-10
      • 2013-09-26
      • 2019-10-14
      • 2018-06-18
      • 2018-06-23
      • 2021-08-13
      • 1970-01-01
      相关资源
      最近更新 更多