簡體   English   中英

Rails回形針不顯示來自Amazon S3的上傳圖像

[英]Rails paperclip doesn't display uploaded images from Amazon S3

我將圖像上傳到Amazon S3,但它們沒有顯示。

我收到此錯誤:

<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>
<Bucket>cb123</Bucket>
<Endpoint>cb123.s3.amazonaws.com</Endpoint>
<RequestId>870BC2E8570EF4E7</RequestId>
<HostId>
yuBkeXxftr7O9Ib0SasFTq8Hlvgc7hkhx9VMr+VwRL74qSDgJ9rqMgEU9noRIQe/
</HostId>
</Error>

這是我的設置:

has_attached_file :image, styles: { medium: "400x400#", small: "250x250#", :url =>':s3_domain_url', :path => '/:class/:attachment/:id_partition/:style/:filename' }

謝謝!

將其添加到application.rb或每個環境的配置文件中:

config.paperclip_defaults = {
  :storage => :s3,
  :s3_host_name => 's3-eu-central-1.amazonaws.com',
  :s3_credentials => {
    :bucket => 'your bucket',
    :access_key_id => 'your access-key-id',
    :secret_access_key => 'your secret-access-key'
  }, 
  :url =>':s3_domain_url',
  :path => '/:class/:attachment/:id_partition/:style/:filename'
}

然后,您可以從模型中刪除:url:path config。

在這里添加我的代碼,因為自上一個答案以來語法略有改變。

您需要將S3_host_name添加到config / environments / development.rb中的config.paperclip_defaults以及production.db中。

以下是完整的代碼段,將us-west-2更改為您的存儲桶所在的任何區域:

config.paperclip_defaults = {
  storage: :s3,
  s3_host_name: 's3-us-west-2.amazonaws.com',
  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'),
  }
}

嘗試將此添加到您的has_attached_file方法所在的模型中:

:url =>':s3_domain_url'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM