Cố gắng tải hình ảnh lên S3 trên Ruby on Rails bằng cách sử dụng carrierwave và đá quý sương mù, hình ảnh được tải lên một cách chính xác nhưng khi tôi thử lưu mô hình chứa thông tin về hình ảnh vừa tải lên lỗi:301 Đã di chuyển vĩnh viễn sau khi tải lên 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>
model User:
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
điều khiển tài khoản
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'
Cảm ơn! Nó hoạt động! Nhưng một số xem xét tôi đã không thực hiện, và tôi sẽ được giải thích dưới đây ... – p1nox
Nhân tiện, tôi đang có một thông báo xác nhận lạ trên tải lên hình ảnh từ xa từ người dùng facebook/twitter, tôi không biết nếu bạn có vài phút để xem nó [Câu hỏi StackOverflow] (http://stackoverflow.com/questions/13730976/image-invalid-on-facebook-twitter-user-image-uploading-to-s3) – p1nox
Điều này dẫn tôi đến câu trả lời - điều buồn cười mặc dù - khu vực trong url của xô của tôi trong giao diện điều khiển aws là khác nhau từ khu vực được liệt kê theo tài sản - do đó, từ khôn ngoan - kiểm tra lại điều đó. Nó giải quyết vấn đề này cho moi. –