【问题标题】:Paperclip image_tag does not show image with S3 and Rails回形针 image_tag 不显示带有 S3 和 Rails 的图像
【发布时间】: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 %>

更新:解决方案

要让它发挥作用,需要做两件事:

  1. s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com" 添加到配置中。这是here 建议的解决方法。
  2. :s3_protocol =&gt; :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


【解决方案1】:

在 aws-adk-2 中,您需要在配置中明确指定 s3 协议 :s3_protocol => :https。

最新版本的回形针依赖于 aws-sdk-2。所以你必须指定协议。

通过这种方式,这将使用 https 保存您的图像 URL。

这个答案会帮助你see the link。你有同样的问题。

【讨论】:

  • 好的,我添加了。现在链接有 https 但错误仍然是一样的......
  • 我更新了我的问题。显然还需要多一步。
  • 很高兴它有帮助:)
猜你喜欢
  • 2015-01-20
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 2019-07-15
  • 2014-10-13
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
相关资源
最近更新 更多