Rails4.2:使用Aws S3/Paperclip Gem未显示图像



拥有一个使用Rails/AWS S3存储的应用程序&回形针宝石。允许用户上传徽标,工作正常,但不能正确显示图像。我得到了图像的名称。我添加了一个image_url,它显示了从amazons3上传的正确url。它也出现在ruby控制台中。

2.0.0-p247 :001 > Job.last.image.url
  Job Load (0.9ms)  SELECT  "jobs".* FROM "jobs"  ORDER BY "jobs"."id" DESC LIMIT 1
 => "http://s3.amazonaws.com/jXXXXX/jobs/images/000/000/011/original/g

Amazon s3 bucket权限已设置为每个人都可以查看。已将图像添加到我的参数方法

application.rb文件中的设置

require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Job1
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'
    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true
    config.paperclip_defaults = { 
        storage: :s3,
        s3_region: 'us-west-2',
        s3_credentials:{
            bucket: ENV['AWS_BUCKET'],
            access_key_id: ENV['AWS_ACCESS_KEY_ID'],
            secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
        }
    }
  end
end

_job.html.erb

<ul id="timeline">
  <a href="#">
    <li class="listing clearfix">
        <div class="image_wrapper">
           <%= link_to job_path(job) do %>
           <%= image_tag job.image.url(:original), class: "img-responsive" %>
          <% end %>
          <%= job.image.url(:original) %>
        </div>
        <div class="info">
            <span class="job_title"><%= link_to job.title, job_path(job) %></span>
            <span class="job_info"><%= job.company %><span>&bull;</span> New York <span>
            &bull;</span>Posted <%= time_ago_in_words(job.created_at) %> ago</span>
        </div>
            <span class="job_type full_time"> Full-Time</span>
        </li>
      </a>
    </ul>
.env file
AWS_BUCKET=joXXXXX
AWS_ACCESS_KEY_ID=AXXXXXXXXXXXXXXTFFQ
AWS_S3_REGION=us-west-2
AWS_SECRET_ACCESS_KEY=yXXXXXXXXXF+gjekFrVz1rG

在浏览器中添加了url并得到了这个

PermanentRedirect必须使用指定的终结点对您试图访问的存储桶进行寻址。请将所有未来的请求发送到此端点。jXXXXXXjXXXXXX.s3.amazonaws.com79XXXXXXZiMAI/J3XXXXXXI2VylhR7Ch3+/Pi+J68gcQ=

但是已经更新了amazons3的权限

端点错误消息显示:

PermanentRedirect必须使用指定的终结点对您试图访问的存储桶进行寻址。请将所有未来的请求发送到此端点。jXXXXXXjXXXXXX.s3.amazonaws.com79XXXXXXZiMAI/J3XXXXXX2VylhR7Ch3+/Pi+J68gcQ=

你的网址是:

s3.amazonaws.com/jXXXXX/jobs/images/

但预计是:

jXXXXX.s3.amazonaws.com

您必须使用s3_host_name var.在回形针默认值上添加端点

config.paperclip_defaults = { 
   s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com", }

更多信息:aws文档

最新更新