Neil
Neil

Reputation: 569

Rails4.2 : Images not showing up using Aws S3/Paperclip Gem

Have an app using Rails / AWS S3 storage & paperclip Gem. Allows users to upload logos , works fine but does not display mages correctly. I get the name of the image. I have added an image_url which displays the correct url uploaded from amazon s3. and it appears in the ruby console as well.

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 permissions have been set to everyone can view. have added image to my params method

settings in application.rb file

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

added the url in the browser and got this

PermanentRedirectThe bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.jXXXXXXjXXXXXX.s3.amazonaws.com79XXXXXXZiMAI/J3XXXXXXI2VylhR7Ch3+/Pi+J68gcQ=

but have updated permission on amazon s3

Upvotes: 0

Views: 586

Answers (1)

catch22
catch22

Reputation: 1693

The endpoint error message says:

PermanentRedirectThe bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.jXXXXXXjXXXXXX.s3.amazonaws.com79XXXXXXZiMAI/J3XXXXXXI2VylhR7Ch3+/Pi+J68gcQ=

Your url is:

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

But it's expected to be:

jXXXXXX.s3.amazonaws.com

You must add the endpoint on your paperclip defaults using the s3_host_name var.

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

More about this: aws documentation

Upvotes: 1

Related Questions