301 Moved Permanently after S3 uploading
Asked Answered
B

2

15

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'
Benevolent answered 3/12, 2012 at 21:18 Comment(0)
B
37
<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/'
  }
Biannual answered 4/12, 2012 at 13:2 Comment(5)
Thanks! It works! But some consideration I haven't made, and I will be explained below...Benevolent
By the way, I'm having a weird validation message on uploading remote image from facebook/twitter users, I don't know if you have a couple of minutes to watch it StackOverflow QuestionBenevolent
This led me to the answer - funny thing though - the region in the url of my bucket in the aws console was different from the region listed under properties - so word the wise - double check that. It solved this issue for moi.Expert
For me, this simplest answer worked as well: #10630930Piero
+1 @Jackson_Sandland, I had the exact same case – really dazzling because for S3 the console's Regions dropdown menu says they don't matter... but then they do for uploads!Cromagnon
B
1

Thanks again to Steffen Opel!

But some consideration I haven't made, my region is US Standard, therefore, my carrierwave initializer looks like this: # :region => # NOT NEEDED BY US STANDARD :endpoint => 'https://s3.amazonaws.com'

This link was the key :D

Benevolent answered 4/12, 2012 at 21:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.