【问题标题】:Rails 5.2.1 Images on View not showing up using Paperclip with AWS even though it displays the correct URL in srcRails 5.2.1 视图上的图像未显示使用带有 AWS 的 Paperclip,即使它在 src 中显示正确的 URL
【发布时间】:2019-05-23 16:41:03
【问题描述】:

这可能只是一个简单而明显的解决方法,虽然我已经阅读了类似的问题并尝试了解决方案,但我似乎无法弄清楚。

我有一个包含图像的 Post 模型。当我上传图片时,它成功上传到 AWS,但是当我查看该帖子时该帖子没有显示它。

我的 S3 存储桶策略已设置,因此每个人都可以查看。

我的 config/environments/production.rb 文件中的 AWS Paperclip 设置为:

 [...]
  config.paperclip_defaults = {
    storage: :s3,
    s3_credentials: {
      bucket: Rails.application.credentials.dig(:aws, :bucket_name), 
      access_key_id: Rails.application.credentials.dig(:aws, :access_key_id), 
      secret_access_key: Rails.application.credentials.dig(:aws,
      :secret_access_key), 
      s3_region: Rails.application.credentials.dig(:aws, :region),
      s3_storage_class: :reduced_redundancy
    }
  }

这是我的帖子模型:

class Post < ApplicationRecord
    belongs_to :user
    has_many :comments, dependent: :destroy
    has_many :likes, dependent: :destroy
    has_many :dislikes, dependent: :destroy
    attr_accessor :uploaded_image_for_io_adapters, :file_name, :top_text, :bot_text


    has_attached_file :image, styles: {   
        square: "250x250#",
        large:  "500x550!"
    }, 
    :convert_options => {
      :medium => "-quality 100 -strip"
    }, 
    default_url: "/images/:style/missing.png"
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
  validates_attachment :image, presence: true
  validates_presence_of :poster
    validates_presence_of :description
    validates :user, presence: true
    validates :user_id, presence: true
end

我的app/views/posts/show.html.erb

<div class="container">
  <center><table border="0" style="max-width: 500px;">
    <tr>
      <td colspan="2"><%= image_tag @post.image.url(:large) %></td>
    </tr>
    <tr>
      <td colspan="2"><h3 class="text-left"><%= @post.description %></h3></td>
    </tr>
    <tr valign="top">
      <td>
    <p>Posted by: <%= @post.poster %></p>
    <p><small class="text-muted"><%= @post.created_at %></small></p>
      </td>
      <td><p class="text-right">
        <div class="voting" style="text-align:center;" id="voting-div">
            <%= render partial: 'posts/likes', locals: {post: @post}  %>
            <%= render partial: 'posts/dislikes', locals: {post: @post}  %>
            <%= render partial: 'posts/post_score', locals: {post: @post}  %>
        </div>
      </p></td>
    </tr>
    <tr>
      <td colspan="2"><p><a href="#" class="glyphicon glyphicon-flag"> Flag as inappropriate</a></p></td>
    </tr>
  </table></center>
  <%= link_to 'Back', url_for(:back) %>
</div>

这是有趣的部分。在没有显示图像的帖子页面上,当我View Page source时,我在图像源下看到: &lt;td colspan="2"&gt;&lt;img src="//bucketname-images.s3.us-east-2.amazonaws.com/posts/images/000/000/016/large/_editedimage_.png?1545616391"/&gt;&lt;/td&gt; 如截图

当我Copy Link Location 并粘贴时,URL 是http://bucketname-images.s3.us-east-2.amazonaws.com/posts/images/000/000/016/large/_editedimage_.png?1545616391。此 URL 在我尝试的每台计算机上显示上传的图像,这让我怀疑问题出在我的视图中。我感谢任何指针

编辑

当我检查控制台时,我看到以下错误:

内容安全政策:页面设置阻止加载 资源在 http://bucketname-images.s3.us-east-2.amazonaws.com/posts/images/000/000/016/square_editedimage_.png?1545616391 (“img-src”)。

【问题讨论】:

标签: ruby-on-rails ruby amazon-web-services amazon-s3 paperclip


【解决方案1】:

如果您在https 中运行您的网站,则需要确保您的S3 链接也通过https 提供,以避免Content Security Policy error。这个答案应该可以让您到达您需要的位置,以便将您的图像链接配置为通过https 提供服务。 Is it possible to configure Paperclip to produce HTTPS urls?

简短的回答是使用s3_protocol 选项:

s3_protocol: :https,或者你的情况是s3_protocol: :http

https://www.rubydoc.info/github/thoughtbot/paperclip/master/Paperclip/Storage/S3

【讨论】:

  • 这成功了!我编辑了config/initializers/content_security_policy.rb 并将img_src :self, :https, :data 更改为img_src :self, :http, :data。我完全错过了这整个时间
  • @i_use_the_internet,请确保您在生产中使用 https。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-10
  • 1970-01-01
相关资源
最近更新 更多