2012-12-03 69 views
10

我試圖使用載波和霧寶石將圖像上載到S3上,圖像上傳正確,但是當我嘗試tu時,保存包含剛上傳圖像信息的模型。錯誤:301在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 

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' 

回答

24
<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-訪問桶西1,但是,由於遺留原因th Ë默認在大多數Amazon S3地區/所有AWS SDKs美國標準,這自動請求路由到使用網絡弗吉尼亞州北部和西北太平洋地區設施映射(見Regions and Endpoints瞭解詳細信息)。

因此,您只需在使用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/' 
    } 
+0

謝謝!有用!但一些考慮,我沒有作出,我會在下面解釋... – p1nox

+0

順便說一下,我有一個奇怪的驗證信息上傳來自Facebook/Twitter用戶的遠程圖像,我不知道你是否有一個幾分鐘看它[StackOverflow Question](http://stackoverflow.com/questions/13730976/image-invalid-on-facebook-twitter-user-image-uploading-to-s3) – p1nox

+1

這導致我的答案 - 有趣的事情 - 在aws控制檯中的我的桶的url中的區域與屬性下列出的區域不同 - 因此請謹慎 - 請仔細檢查。它解決了moi的這個問題。 –

1

再次感謝Steffen Opel

但一些考慮我沒有做,我區被美國標準,因此,我carrierwave初始化看起來是這樣的: #:區域=>#不需要用美國標準 :端點=>「https://s3.amazonaws.com

link是關鍵:D