S3上传后永久301移到 [英] 301 Moved Permanently after S3 uploading

查看:377
本文介绍了S3上传后永久301移到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着使用carrierwave和雾宝石上传图片到S3上的Ruby on Rails,图像正确上传但是当我尝试涂保存,包含有关刚刚上传即时得到这个错误的图像信息模型:

  EXCON ::错误:: MovedPermanently在UserController的#秀
应用程序/模型/ user.rb:46:在'process_image_with_key
应用程序/控制器/ user_controller.rb里:12:在'秀'

< EXCON ::回应:0x007f97846a3c18 @body =?< XML版本= \1.0 \编码= \UTF-8 \> \ N'LT;错误>< code取代; PermanentRedirect< / code><消息>您正试图访问的桶必须使用指定的端点解决请将今后所有申请到这个端点< /消息>
 

User模型:

  mount_uploader:图像,​​AvatarUploader

高清IMAGE_NAME
  File.basename(image.path || image.filename)如​​果图像
结束

高清process_image_with_key(钥匙)
  除非key.nil?
    self.key =关键
    self.remote_image_url = self.image.direct_fog_url(with_path:真)
    self.save!
  结束
结束
 

AvatarUploader:

 #编码:UTF-8

类AvatarUploader< CarrierWave ::上传::基地

  包括CarrierWaveDirect ::上传

  包括CarrierWave :: RMagick

  #包括链轮佣工的Rails 3.1+资产管道兼容性:
  包括链轮::助手:: RailsHelper
  包括链轮::助手:: IsolatedHelper

  包括CarrierWave :: MIMETYPES
  过程:set_content_type

  版本:拇指做
    过程resize_to_fill:[50,50]
  结束

结束
 

用户控制

 高清节目
  @user = User.find_by_id(PARAMS [:ID])
  @ user.process_image_with_key(PARAMS [:密钥])
  @uploader = User.new.image
  @ uploader.success_action_redirect = user_url(@ user.id)
结束
 

carriwerwave初始化

  CarrierWave.configure办|配置|
  config.fog_credentials = {
    :供应商=> AWS,
    :aws_access_key_id => ENV ['AWS_ACCESS_KEY_ID'],
    :aws_secret_access_key => ENV ['AWS_SECRET_ACCESS_KEY'],
    :地区=> 美西1'
  }
  config.fog_directory = ENV ['AWS_FILE_BUCKET']
  config.fog_attributes = {'的Cache-Control'=>最大年龄= 315576000'}
结束
 

的Gemfile

 创业板carrierwave
创业板rmagick
宝石'雾'
创业板carrierwave_direct
 

解决方案

<$c$c><Error><$c$c>PermanentRedirect</$c$c><Message>The斗您尝试访问 必须使用指定的端点解决。请发送到所有将来的请求 此端点和LT; /消息&GT;&LT; /错误&GT;

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

因此​​,您只需要使用S3 API,如之前显式指定桶区域的终点为的美西-1 的:

config.fog_credentials = {     :供应商=&GT; AWS,     :aws_access_key_id =&GT; ENV ['AWS_ACCESS_KEY_ID'],     :aws_secret_access_key =&GT; ENV ['AWS_SECRET_ACCESS_KEY'],     :地区=&GT; 美西1'     :端点=&GT; https://s3-us-west-1.amazonaws.com/   }

Im trying to upload images to S3 on Ruby on Rails using carrierwave and fog gems, images are uploaded correctly but when I try tu save the model containing information about the image that was just uploaded Im getting this error:

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>

User model:

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

User controller

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 initializer

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'

解决方案

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

This is a frequently encountered issue: You are trying to access a bucket in region us-west-1, however, for legacy reasons the default Amazon S3 region in most/all AWS SDKs is US Standard, which automatically routes requests to facilities in Northern Virginia or the Pacific Northwest using network maps (see Regions and Endpoints for details).

Therefore you simply need to specify the endpoint of your buckets region explicitly before using the S3 API, e.g. for 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/'
  }

这篇关于S3上传后永久301移到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆