![](/img/trans.png)
[英]Credentials missing when uploading photos with Paperclip and Amazon s3
[英]Paperclip is missing the Protocol (https) with Amazon S3
在production.rb中:
config.paperclip_defaults = {
s3_host_name: "s3.#{ENV.fetch('AWS_REGION')}.amazonaws.com",
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
我在初始化器/paperclip.rb中沒有任何內容。
在我的模型中:
class MyModel < ApplicationRecord
has_attached_file :photo, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
}
validates_attachment_content_type :photo, content_type: /\Aimage\/.*\Z/
def photo_url=(url)
self.photo = URI.parse(url)
end
end
然后我測試一下:
m = Model.new
m.photo_url = "https://s3.us-east-2.amazonaws.com/mybucket/sports-grill-miami.jpg"
m.save!
m.photo.url(:thumb)
"//s3.us-east-2.amazonaws.com/mybucket/buckets/photos/000/000/005/thumb/sports-grill-miami.jpg?1495237443"
為什么缺少HTTPS協議? 這使我的Android應用程序崩潰,因為它需要協議才能連接到URL。 我需要對URL進行硬編碼還是Paperclip可以處理?
您需要將協議顯式添加到您的配置中:
:s3_protocol => :https
您需要在paperclip
配置中指定方案,如下所示:
config.paperclip_defaults = {
s3_host_name: "s3.#{ENV.fetch('AWS_REGION')}.amazonaws.com",
storage: :s3,
:s3_protocol => :https, # <- added this
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
:s3_protocol => :https
將方案https
分配給為您的Amazon s3資產生成的URL。 有關更多詳細信息,請參考文檔 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.