RoR, Paperclip and S3 upload image error - ruby-on-rails

I deployed my rails app and I used S3 to host images that I will be uploading using paperclip. Here is the error that I keep getting even though I did permit :photo in my controller params
2015-12-08T13:40:00.439949+00:00 app[web.1]: [AWS S3 403 0.042095 0 retries] put_object(:acl=>:public_read,:bucket_name=>"customlized",:content_length=>30237,:content_type=>"image/jpeg",:data=>Paperclip::UploadedFileAdapter: shanab.jpg,:key=>"products/photos/000/000/009/original/shanab.jpg") AWS::S3::Errors::AccessDenied Access Denied
2015-12-08T13:40:00.439953+00:00 app[web.1]:
2015-12-08T13:40:00.441272+00:00 app[web.1]: (0.9ms) ROLLBACK
2015-12-08T13:40:00.442792+00:00 app[web.1]:
2015-12-08T13:40:00.442793+00:00 app[web.1]: AWS::S3::Errors::AccessDenied (Access Denied):
2015-12-08T13:40:00.442794+00:00 app[web.1]: app/controllers/products_controller.rb:35:in `block in create'
2015-12-08T13:40:00.442795+00:00 app[web.1]: app/controllers/products_controller.rb:34:in `create'
My production.rb
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']
}
}
I did config it on Heroku to what S3 gave me as credentials
My product.rb
has_attached_file :photo ,
:storage => :s3,
:s3_credentials => Proc.new{|a| a.instance.s3_credentials },
styles: { medium: '300x300>', thumb: '100x100>' }
def s3_credentials
{:bucket => "Deleted", :access_key_id => "Deleted", :secret_access_key => "Deleted"}
end

Try moving your AWS credentials out of your paperclip_defaults and the product.rb model, and add to an aws.yml file in the config directory:
production:
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
(also make sure you have gem 'aws-sdk', '~> 1.6' in your Gemfile)
More documentation on S3 with Paperclip here: https://github.com/thoughtbot/paperclip/wiki/Paperclip-with-Amazon-S3

Related

File upload to Amazon S3 using paperclip

I'm uploading files to S3 using paperclip so would like to know if i can set the path something like,
:path => "/advertisements/:username/:filename”
the thing is that :usename is from other model; i'm uploading files on model_2 and :username comes from model_1. How can i set the path to indicate the :username
Sample:
:path => "/advertisements/#model_1.username/:filename”
Any ideas?
Thanks in advance!
Here is nice explanation:
please view the answer.
Rails 4, Paperclip, Amazon S3 Config Amazon Path
Model:
#Image Upload
Paperclip.options[:command_path] = 'C:\RailsInstaller\ImageMagick'
has_attached_file :image,
:styles => { :medium => "x300", :thumb => "x100" },
:default_url => "****",
:storage => :s3,
:bucket => '****',
:s3_credentials => S3_CREDENTIALS,
:url => "/:image/:id/:style/:basename.:extension",
:path => ":image/:id/:style/:basename.:extension"
config/application.rb
# Paperclip (for Amazon) (we use EU servers)
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-eu-west-1.amazonaws.com'
}
config/s3.yml
Amazon AWS Config
development:
access_key_id: **********
secret_access_key: **************
bucket: ****
production:
access_key_id: ***********
secret_access_key: ***********
bucket: ****
I hope this is what you're looking for :)

Rails Access Deined when uploading image on Heroku to Amazon s3 with paperclip

i try to upload image using amazon S3, but i have this error:
AWS::S3::Errors::AccessDenied (Access Denied)
i used paperclip configuration:
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']
}
}
and i set a permission like this
thank you !
EDIT:
I created another bucket and it works, but now i have this error:
NoMethodError (undefined method `first' for nil:NilClass):
production.rb/development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_protocol => 'http',
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
While Creating a bucket Select region as US-Standard and use above config.
Try this it worked for me.Hopefully would work for you too.

AWS Credentials Missing when pushing to Heroku

Answered: I misspelled AWS_SECRET_ACCESS_KEY when setting the environment variables for Heroku. I left off the second S.
I'm building a Rails 4 application, and using Paperclip and AWS for image uploading. Every time I try to upload an image, I get AWS::Errors::MissingCredentialsError (Missing Credentials in the Heroku logs.
In my gem file I have
gem 'paperclip'
gem 'aws-sdk'
In my production.rb file I have
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']
}
}
I have an initializer named aws-sdk.rb that only has this in it:
AWS.config({
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
})
And in my relevant model file I have
has_attached_file :picture,
:storage => :s3,
:s3_credentials => {
:bucket => 'MyBucketName',
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
},
styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
}
I didn't think that I needed to explicitly include the s3 credentials with the has_attached_file method? I tried it anyway. Still not working.
When I run heroku config it shows my access key, key id, and bucket names correctly.
I have no idea what's going wrong.

Paperclip on heroku with S3 - not working

I am using paperclip to handle image uploads in my Rails app. All works fine running in the development environment locally using the File Storage.
Now I am trying to get this working using S3 (because the app is to run on Heroku). I have set up a bucket and set the appropriate parameters in development.rb and production.rb as per the instructions here: https://devcenter.heroku.com/articles/paperclip-s3
When I start the server, I get the following error:
/Users/ganzogo/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.13/lib/rails/railtie/configuration.rb:85:in `method_missing': undefined method `paperclip' for #<Rails::Application::Configuration:0x007fcb8b952000> (NoMethodError)
from /Users/ganzogo/Documents/acknowledgement/true-rails/config/environments/development.rb:41:in `block in <top (required)>'
And then it crashes.
The line referred to in the error is:
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']
}
}
I get exactly the same error if I try to run on Heroku. Has anyone been through this and have any idea what I have missed?
It looks like you may have a typo in your configuration block. Try changing config.paperclip.defaults = {...} to config.paperclip_defaults = {...} and that should solve your problem.
We got this working on one of our live apps
The difference is you need to put the credentials into the model itself. Here's what we've got:
#app/models/image.rb
#Image Upload
Paperclip.options[:command_path] = 'C:\RailsInstaller\ImageMagick'
has_attached_file :image,
:styles => { :medium => "x300", :thumb => "x100" },
:default_url => "*******",
:storage => :s3,
:bucket => '******',
:s3_credentials => S3_CREDENTIALS
#app/config/application.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-eu-west-1.amazonaws.com'
}
In Heroku, you'll need to add the various environment variables to your config settings

Heroku Paperclip S3 error => AWS::S3::Errors::SignatureDoesNotMatch

Paperclip+S3 work on the local machine but dont work on Heroku. It says "AWS::S3::Errors::SignatureDoesNotMatch"
2012-04-20T21:24:37+00:00 app[web.1]: Parameters: {"utf8"=>"Γ£ô",
"authenticit y_token"=>"MHt20N64y9L24Ga2y6gGj8hX06U+td17XIRdqMadcCQ=",
"post"=>{"title"=>"tes t", "organisation_id"=>"1", "content"=>"weeqw",
"attachments_attributes"=>{"0"=>
{"pic"=>#>}}},
"commit"=>"Create Post"} 2012-04-20T21:24:41+00:00 app[web.1]:
Completed 500 Internal Server Error in 344 6ms
2012-04-20T21:24:41+00:00 app[web.1]: 2012-04-20T21:24:41+00:00
app[web.1]: AWS::S3::Errors::SignatureDoesNotMatch (Th e request
signature we calculated does not match the signature you provided. Che
ck your key and signing method.):
Here is my attachment.rb model:
has_attached_file :pic, :styles => { :medium => "500x280>",:slide => "180x260>", :thumb => "150x100#" },
:storage => :s3,
:bucket => 'sample_bucket',
:s3_credentials => {
:access_key_id => 'ASDDSAMPLEKEYDFSDF',
:secret_access_key => 'Dfsdf887-sample-secret-key-dfdsasd',
}
What might be the reason of this error?
I've paperclip 2.4.5 and here is the definition inside the model:
has_attached_file :logo,
:path => "assets/:class/:attachment/:id/:style.:extension",
:storage => :s3,
:bucket => ENV['S3_BUCKET'],
:s3_permissions => 'authenticated-read',
:s3_protocol => "http",
:s3_credentials => { :access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET'] },
:styles => { :index => "72x72>",
:billboard => "120x120>" }
I was having some similar issues with Heroku and found this https://rubygems.org/gems/paperclip-aws.
This solved my problem.

Resources