【发布时间】:2018-06-13 06:12:58
【问题描述】:
我在我的 Rails (4.2) 应用程序中使用 Paperclip 上传 S3 图像。一切正常,除了 image_tag 不显示图像而是图像的标题这一事实。我错过了什么?
recipe.rb
class Recipe < ActiveRecord::Base
has_attached_file :image, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
}
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validates_with AttachmentSizeValidator, attributes: :image, less_than: 1.megabytes
end
食谱 show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @recipe.name %>
</p>
<div class="col-xs-12" style="height:400px">
<%= image_tag @recipe.image.url(:original) %>
</div>
<%= link_to 'Edit', edit_recipe_path(@recipe) %> |
<%= link_to 'Back', recipes_path %>
更新:解决方案
要让它发挥作用,需要做两件事:
- 将
s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com"添加到配置中。这是here 建议的解决方法。- 将
:s3_protocol => :https添加到配置中,因为 aws-sdk-2 明确需要此功能(如 cmets 中所述)。
【问题讨论】:
-
一切看起来都很好,您可以通过在浏览器中粘贴 URL 来检查 s3 URL 上是否存在图像
-
是的,它就在那里。如果我在我的 s3 控制台中找到我的存储桶,我会找到图像并可以在那里检索 url。它是:s3.eu-central-1.amazonaws.com/powerrecipes/recipes/images/000/…
-
尝试通过检查 image_tag 将这个 url 直接放在浏览器中。浏览器控制台中的永久重定向错误?
-
我在 Chrome 中检查了页面的元素,它显示了 image_tag 的以下 URL:。如果我尝试在浏览器中打开 URL,它会显示 PermanentRedirect 错误和“您尝试访问的存储桶必须使用指定的端点进行寻址。请将所有未来的请求发送到此端点。”
-
它是因为您的图片网址没有采用 https。您能检查一下您发布的上述代码是否正确
标签: ruby-on-rails amazon-web-services amazon-s3 paperclip