images uploaded to aws, but can't be viewed in the view - ruby-on-rails

I have just integrated AWS with my rails/heroku app and I am using paperclip. I am able to upload files (photo's) and see them in AWS, however they are not showing up in the view. I am not getting any errors, and have not found a working solution in other posts.
It seems I am able to view the image in a browser, and that permissions are set to public:
I suspect that I may have my region wrong, in the url of my aws dashboard the region says region=us-west-2 yet googling and reading through other forums and posts on SO leads me to believe that if I am in the US my region should be set to us-east-1 - currently I have it set to the latter. Not sure if this is the problem.
Here is a link to the image on AWS:https://s3.amazonaws.com/giving-tree-images/avatars/1/medium/02108_navajoland_1440x900.jpg
here is the code pertaining to aws/paperclip in my model:
:storage => :s3,
# :s3_host_name => "s3-us-east-1.amazonaws.com",
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
},
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:default_url => "default_img.png"
and I am displaying in the view like this:
<%= image_tag #user.avatar.url(:medium) %>
I would love it if someone could point me in the right direction... Any additional info needed, please let me know!

I also faced the same issue, so this is how I solved it:
Create a new initializer file i.e. config/initializers/paperclip.rb
Add following in this file
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'
To get details about your region, go to aws
Amazon Web Service - Check your host name
Note : Replace s3-us-west-2.amazonaws.com with your own host name
Reference : https://devcenter.heroku.com/articles/paperclip-s3

Have you enable the read permission to everyone?
It gave me access-denied error when accessed to the link.

Related

Paperclip not showing image even though URL/images in correct places

Using Paperclip to attach avatars to User profiles for my rails application. I followed the instructions on the paperclip github to initialize and attach to my app.
I have an image in the public/images/medium/missing.png and for both cases (when I upload or when I fallback on the default) I get no image. I've checked my directory and there is an image where it says it is looking but does not grab it. Additionally when I have tried uploading images, I know the image is uploaded correctly because when calling the User in rails console shows all the information properly attached.
I am calling the image in my view like:
<%= image_tag(#blog.user.avatar.url(":medium"), :class => "image-circle avatar") %>
my Paperclip declaration in the User model looks like the following:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#"}, :default_url => "/public/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
Really not sure what is going on. The route errors that appear when I inspect the improperly loaded image point directly to the image in my local server. And the fact that it can't grab either the missing or the uploaded file also has me at a loss. Any help would be super appreciated!!
And for good measure the output when I examine a user with an uploaded avatar:
avatar_file_name: "11390219_10206114805925816_6595348111261743639_n.j...", avatar_content_type: "image/jpeg", avatar_file_size: 101926, avatar_updated_at: "2015-07-10 18:51:44">
Thanks in advance!
EDIT
This is the URL that is providing the 404 error:
http://localhost:3000/images/medium/missing.png
while in my local directory it goes "root/public/images/medium/missing.png"
not sure how its not grabbing it, unless I am just missing something really obvious somewhere. (i tried hard routing the public in there as well, but to no avail).
EDIT
There is the possibility that you're simply not serving the static assets, add:
config.serve_static_assets = true
to your development.rb
ORIGINAL POST
In you application.rb ( or an environment specific file ), you should have a config.paperclip_defaults = { ... }, here is the link in the docs: https://github.com/thoughtbot/paperclip#defaults
Here is an example one, using fog:
config.paperclip_defaults = {
:storage => :fog,
:fog_credentials => {
:provider => "Local",
:local_root => "#{Rails.root}/public"
},
:fog_directory => "",
:fog_host => "localhost:3000"
}
Do you have something like that in your application? I just tested on an app of mine, and I was able to upload an image, but not to see any without the paperclip_defaults hash. Also, don't forget to restart your app after you update the config files. I hope this helps!
Have you tried killing the quotes around the image style? Around :medium?
<%= image_tag(#blog.user.avatar.url(:medium), :class => "image-circle avatar") %>
As show here in the Paperclip Docs:
https://github.com/thoughtbot/paperclip#show-view
Same problem, solved it by reinstalling imagemagic with brew
Details: In case you're imagemagic, try to update the imagemagic and if it asks you to link it,
Try with this:
brew link --overwrite imagemagick
It worked for me. Hope its helpful

Paperclip/Rails not reading ENV variables with AWS information

I am trying to use paperclip with S3 AWS. I have set the secret keys and bucket name in the .bash_profile but Rials/Paperclip cannot seem to read them as i get the following error when i try to upload an image in development...
'missing required :bucket option'
If i replace the ENV['S3_BUCKET_NAME'] with the actual name, i then get the following...
'Missing Credentials. Unable to find AWS credentials'
Here is the set-up in the model...
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" },
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
},
:s3_host_name => 's3-eu-west-1.amazonaws.com'
I have tried putting the credentials in the development config file in Rails but i have the same errors. Do i need to tell rails where to look for the ENV variables or am I doing something else wrong here? It seems others are having similar issues but i cannot find a solution.
Thanks for reading.
Just store the AWS bucket, key and key_id in a YAML file and in 'config/initializers', create a new file to read the YAML file contents and set the corresponding ENV variables. Don't forget to restart your rails server
Example
google_client = YAML.load_file("#{Rails.root.join('config/google_client.yml')}")
ENV['GOOGLE_APP_NAME'] = google_client['APP_NAME']
ENV['GOOGLE_CLIENT_ID'] = google_client['CLIENT_ID']
ENV['GOOGLE_CLIENT_SECRET'] = google_client['CLIENT_SECRET']
ENV['GOOGLE_CLIENT_SCOPE'] = google_client['CLIENT_SCOPE']
Typically, we do not push the aws info in Github commit but just ssh to the remote server and place the YAML file in the config folder in our Rails app. So only people who have access to your server can read your aws credentials and not all people even if they have Github access.
tell me if this solves your problem.

Paperclip is not generating url properly for CNAME

I'm tying to get a cloudfront distribution wired up with a custom domain to paperclip but it's not generating my url properly. Here is my configuration:
# initializers/paperclip_defaults.rb
Paperclip::Attachment.default_options.merge!({
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/amazon_s3.yml",
:bucket => "my-image-bucket",
:path => "production/attachments/:attachment/:id/:style/:basename.:extension",
:url => ":s3_host_alias",
:s3_headers => {'Cache-Control' => 'max-age=2147483648'},
:s3_protocol => :https,
:s3_host_alias => "cdn.mydomain.com",
})
According to the docs and every blog post I've read this should be correct. The problem is that it winds up generating this:
https://s3.amazonaws.com/my-image-bucket/production/products/images/44/original/my-image.jpg
The only way I've gotten it to use the CNAME is by specifying s3_host_name which according to the documentation is only used for the Tokyo region. And this also breaks uploads. Any ideas as to what I'm doing wrong?
I'm using Paperclip 3.5.2.
UPDATE
I'm debugging inside attachment.rb#url and here's what I'm seeing. Paperclip::Attachment.default_options[:url] returns cdn.mydomain.com which is correct. However, #options[:url] is returning :s3_path_url instead of s3_host_alias. For some reason that default option is not being merged in properly. However the s3_host_alias option is since #options[:s3_host_alias] is returning cdn.mydomain.com as expected.
So it turns out that :s3_alias_url is the correct token to use for the url and not :s3_host_alias. Not sure where that's documented though. Had to look at s3.rb to figure it out.

Paperclip uploads on Heroku using S3

I'm sorry to rehash an old gripe but I'm at my wits end and not sure where to go next. I am using Paperclip on Heroku and have S3 uploads configured. I was able to get things working in my local development environment but once it's running on Heroku I run into this error:
AWS::S3::Errors::PermanentRedirect (The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
I've googled this error and read through the Heroku documentation and I believe I have everything set up correctly. I initially thought that my problems stemmed from having my bucket in the s3-us-west-1.amazonaws.com region, but I'm not convinced anymore.
Here are the relevant parts of my Heroku config:
AWS_REGION: us-west-1
S3_BUCKET_NAME: my-super-awesomely-amazing-bucket
From my config/environments/production.rb file:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
My paperclip.rb initialize file:
if Rails.env.production?
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-1.amazonaws.com'
end
And my paperclip config from the relevant model:
has_attached_file :document,
:styles => { },
:default_url => "/image_styles/:style/missing.png"
So...what am I doing wrong here? At this point I'm sure I've missed something obvious but I'm stumped on where to go from here. I feel like I've assiduously configured everything and yet that PermanentRedirect error keeps coming up.
Bucket
This might not be the direct solution, but we've found that you have to include the bucket option outside of your s3_credentials block:
#config/environments/production.rb
config.paperclip_defaults = {
storage: :s3,
s3_host_name: 's3-eu-west-1.amazonaws.com',
s3_credentials: {
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
},
bucket: ENV['S3_BUCKET_NAME']
}
This is working 100% for us on Heroku, but whether it wil work for you (as your bucket is in a different region) is a different matter
If you need more help, ask a comment and I'll gladly give you some ideas
Try this link http://adamthedeveloper.blogspot.in/2014/02/awss3permanentredirect-bucket-you-are.html . Previously this happened to Europe region buckets only and get resolved by setting AWS::S3::DEFAULT_HOST. Hope this solve your issue.

using Amazon S3 with the paperclip gem

Is it good practice to simply post my access key id/secret access key directly into the model? For example...
has_attached_file :pic, :s3_credentials => {
:bucket => 'MY_BUCKET_NAME',
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename",
:access_key_id => "12345Secret#blahblah",
:secret_access_key => "12345###"
}
This actually works, however, when I duplicate the image to another model...it doesnt upload.
i.e. #other_user.pic = #user.pic (to copy the image to the other User)
For instance if the image that works is: S3Amazon/pic/01.working.jpg
the copied image has a one number increase in the file like so: S3Amazon/pic/02.working.jpg
This image does not open, been searching all week for a way to make this work.
Try
#other_user.pic = #user.pic
#other_user.save!
And make sure, if you're doing this from the console, that you do #other_user.reload before checking urls

Resources