HolyMoly
HolyMoly

Reputation: 2080

Unable to render images in view (paperclip/AWS)

I have been scouring through SO posts for the last day and a half, and have asked another question related to this issue here, and have yet to come up with a solution. I am unable to render my images, which are being correctly uploaded to AWS in my view using <%= image_tag @user.avatar.url(:medium) %> as specified in the documentation.

Looking in my DB, all of the fields related to the avatar are indeed populated with:

avatar_file_name  => meditation.jpg       
avatar_content_type => image/jpeg                   
avatar_file_size => 109992 
avatar_updated_at => 2016-08-04 06:48:31.361434

and the image can be viewed in the browser by double clicking on the image's link in my amazon bucket: https://s3.amazonaws.com/giving-tree-images/Users/sgau677/giving_tree/public/avatars/12/medium/meditation.jpg.jpg

As you can see, for some reason a double file extension is being appended, which is a separate issue (that I welcome feedback on) but which does not seem to be the issue, as I am not able to render in the view uploaded images with a single file extension either.

So with the images being uploaded, the db fields being populated, and no errors - I am at a loss as to why I cannot render them in the view. I suspect the issue is either 1) the path -which looks like this in my model:

has_attached_file :avatar, 
:styles => { medium: "300x300#", thumb: "100x100#" },
:convert_options => {
:thumb => "-quality 75 -strip" }, 
:s3_host_name => "s3.amazonaws.com",
:path  => ":rails_root/public/:attachment/:id/:style/:filename.:extension",
:url => ":attachment/:id/:style/:filename.:extension",
:default_url => "default_img.png",
:storage => :s3,
:s3_credentials => {
  :bucket => 'giving-tree-images',
  :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
  :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
},
 :s3_permissions => {
  :original => 'public-read'
} 
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

attr_accessor :avatar_file_name, :avatar_content_type, :avatar_file_size, :avatar_updated_at

Which as you can see seems to be correct (unless I should be taking out the :rails_root bit in my model?)

or 2) I need to somehow save the location of the images in a separate model (which I have seen other ppl do, but am not sure it is required, as others seem not to have done this - granted they may just not have mentioned that they did). This being a step has not been in any of the docs I have read however.

3) I also wonder if there is possibly a separate process that I need to be doing in development as opposed to production?

or 4) I am using rails 4.2.3 and have read in comments that paperclip does not support versions of aws > 2.00 - however the Heroku docs - instructed to use 2.3...so perhaps this is the issue?

gem "paperclip", "~> 5.0.0"
gem 'aws-sdk', '~> 2.3'

I have also set up, in following other posts, a config/asw.yml file that looks like this:

development:
  access_key_id:  <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>

production:
  access_key_id:  <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>

although I am not sure it is being read (where the ENV var's are stored in secrets.yml, as well as both locally and on heroku via terminal) and a config/s3.yml file that looks like this:

include 'secrets.yml'

development:
 access_key_id:  <%= ENV['AWS_ACCESS_KEY_ID'] %>
 secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
 bucket: <%= ENV["S3_BUCKET_NAME"]%>

production:
 access_key_id:  <%= ENV['AWS_ACCESS_KEY_ID'] %>
 secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
 bucket: <%= ENV["S3_BUCKET_NAME"]%>

an initializers/s3.rb

if Rails.env == "production"
 # set credentials from ENV hash
 S3_CREDENTIALS = { :access_key_id => ENV['AWS_ACCESS_KEY_ID'], :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'], :bucket => "giving-tree-images"}
else
 # get credentials from YML file
 S3_CREDENTIALS = Rails.root.join("config/s3.yml")
end

and modified my both environments/development.rb and my environments/production.rb to include:

  config.paperclip_defaults = {
  storage: :s3,
  :s3_host_name => "s3.amazonaws.com",
  s3_credentials: {
    bucket: ENV.fetch('S3_BUCKET_NAME'),
    access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
    secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
    s3_region: ENV.fetch('AWS_REGION'),
  }
}

...as per multiple other posts combined suggested. It is quite possible I do not need all of this, so if anyone can help as to what I should be getting rid of, and why I cannot render images in the view - I would REALLY appreciate it as this is maddening.

Upvotes: 2

Views: 147

Answers (1)

aenw
aenw

Reputation: 851

You are putting extension after :filename for both the :path and the :url -- but you shouldn't.

:filename includes the extension. That is why you are getting the extension twice.

You have this in your model: :path => ":rails_root/public/:attachment/:id/:style/:filename.:extension", :url => ":attachment/:id/:style/:filename.:extension",

and after you remove .:extension, you should have this: :path => ":rails_root/public/:attachment/:id/:style/:filename", :url => ":attachment/:id/:style/:filename",

Upvotes: 0

Related Questions