Paperclip缺少Amazon S3的协议(https)

时间:2021-07-16 21:07:36

In 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'),
    }
}

I don't have anything in the initializers/paperclip.rb.

我在initializers / paperclip.rb中没有任何内容。

In my model:

在我的模型中:

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

And then I test it out:

然后我测试出来:

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" 

Why is the HTTPS protocol missing? This is crashing my android application because it requires a protocol to connect to URL. Do I need to hardcode the URL or can Paperclip handle this?

为什么缺少HTTPS协议?这会使我的android应用程序崩溃,因为它需要一个协议才能连接到URL。我是否需要对URL进行硬编码或者Paperclip可以处理此问题吗?

2 个解决方案

#1


4  

You need to explicitly add the protocol to your configuration:

您需要将协议明确添加到配置中:

:s3_protocol => :https

#2


4  

You need to specify the scheme on paperclip configuration as below:

您需要在回形针配置中指定方案,如下所示:

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 will assign the scheme https to the url's generated for your amazon s3 assets. Refer to documentation for more details.

:s3_protocol =>:https会将方案https分配给为您的亚马逊s3资产生成的网址。有关详细信息,请参阅文档。

#1


4  

You need to explicitly add the protocol to your configuration:

您需要将协议明确添加到配置中:

:s3_protocol => :https

#2


4  

You need to specify the scheme on paperclip configuration as below:

您需要在回形针配置中指定方案,如下所示:

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 will assign the scheme https to the url's generated for your amazon s3 assets. Refer to documentation for more details.

:s3_protocol =>:https会将方案https分配给为您的亚马逊s3资产生成的网址。有关详细信息,请参阅文档。