Paperclip 缺少与 Amazon S3 的协议 (https)
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'),
}
}
我在 initializers/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
分配给为您的亚马逊 s3 资产生成的 url。详情请参阅 documentation。
在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'),
}
}
我在 initializers/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
分配给为您的亚马逊 s3 资产生成的 url。详情请参阅 documentation。