Heroku Paperclip S3 error => AWS::S3::Errors::SignatureDoesNotMatch - ruby-on-rails

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.

Related

RoR, Paperclip and S3 upload image error

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

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.

Paperclip + AWS S3, Prefixing remote path with my local path

I'm using Paperclip with a Rails 4 app and Amazon S3 storage. On my development machine, the site is running at
/Users/Jeff/Sites/example.com/web
When I upload a file with Paperclip to S3, the remote path in S3 inherits my local folder structure.
http://s3.amazonaws.com/example_com_bucket/Users/Jeff/Sites/example.com/web/public/assets/uploads/my_class/8/medium/some_image.png?1383060287
Why is this happening? How do I strip that part out? I tried changing the :path property but that only seemed to affect the "application" part of the path (e.g. after /assets/uploads) My site is still in development, so I don't care about having to preserve links.
My config is...
config.paperclip_defaults = {
:storage => :s3,
:path => '/:class/:attachment/:id_partition/:style/:filename',
:s3_credentials => {
:bucket => 'example_com_bucket',
:access_key_id => '...',
:secret_access_key => '...'
}
}
I had this exact same issue when I was using the :url parameter where I should have been using the :path parameter:
has_attached_file :primary_photo,
:styles => ...,
:storage => :s3,
:s3_host_name => 's3-us-west-2.amazonaws.com',
:s3_credentials => 'config/s3.yml',
:url => '/product/:attachment/:id/:style/:filename'
I fixed it by changing my config to this:
has_attached_file :primary_photo,
:styles => ...,
:storage => :s3,
:s3_host_name => 's3-us-west-2.amazonaws.com',
:s3_credentials => 'config/s3.yml',
:path => '/product/:attachment/:id/:style/:filename'

Rails Paperclip S3 - missing required :bucket option

I'm trying to use Amazon S3 for Paperclip attachments. First, I'm trying to get it to work in development environment on my iMac.
I have created the Amazon buckets = ndeavor-dev and ndeavor-pro. In the code below, I have substituted the bucket name and keys. I have the gem's paperclip and aws-sdk.
The error I get is:
ArgumentError at /attachments
missing required :bucket option
I have tried this in my config/environments/development.rb:
config.paperclip_defaults = {
:storage => :s3,
:s3_protocol => 'http',
:bucket => ENV['AWS_BUCKET'],
:s3_credentials => {
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
And I tried this (moving the :bucket):
config.paperclip_defaults = {
:storage => :s3,
:s3_protocol => 'http',
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Thanks for the help!
Like dcro says, you need to set the AWS_BUCKET environment variable properly.
To do this, create a file at config/application.yml and put the following in it, using your Amazon credentials:
AWS_ACCESS_KEY_ID: "whatever_the_key_is"
AWS_SECRET_ACCESS_KEY: "whatever_the_secret_is"
AWS_BUCKET: "ndeavor-dev"
Then restart your server. You'll then be able to use your models something like this:
has_attached_file :attachment ,
:storage => :s3 ,
:s3_credentials => {:bucket => ENV['AWS_BUCKET' ],
:access_key_id => ENV['AWS_ACCESS_KEY_ID' ],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']},
:s3_protocol => "https" ,
:s3_host_name => "s3-eu-west-1.amazonaws.com"

Rails paperclip amazon aws s3 gem, how to change image url?

In my model I have:
has_attached_file :image,
:storage => :s3,
:styles => { :original => ["300x250>", :png], :small => ["165x138>", :png], :mini => ["120x120>", :png] },
:path => 'images/vind/:style/:id/:basename.:extension',
:url => 'images/vind/:style/:id/:basename.png',
:bucket => 'konkurrencerher',
:s3_credentials => {
:access_key_id => 'x',
:secret_access_key => 'x'
}
The problem is just that there is added the amazon s3 hostname to the url in view.
I have a solution to this, but is a bit ugly:
<%= image_tag(kon.photo.image.url(:small).gsub("http://s3.amazonaws.com/konkurrencerher", ""), :class => 'koni') %>
But, how is it possible to define the image url in the model, without the Amazon S3 hostname?
Take a look at Paperclip::Storage::S3, especially on the :s3_host_alias.
You can try configuring your has_attached_file with the following additional options
:url => ':s3_alias_url',
:s3_host_alias => "example.domain.net"
Hope this helps.
My solution created a file in the initializers map with this:
Paperclip.interpolates(:s3_path_url) { |attachment, style|
"#{(attachment.path).gsub("images/", "")}"
}
And then the url should be:
:url => ':s3_path_url'
This is a much better solution.

Resources