Rails Paperclip S3 - missing required :bucket option - ruby-on-rails

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"

Related

AWS::S3::Errors::InvalidAccessKeyId in rails using paperclip gem

I get this error on signup with an uploaded image but I've already set my AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID in heroku config and triple checked that they are correct.
here is what I have in my development environment
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
}
}
here is my production environment
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Here is my aws.rb initializer (I get a missing credentials error without these two lines)
AWS.config(
access_key_id: 'your_access_key',
secret_access_key: 'your_secret_access_key')
and finally here is my paperclip.rb initializer
# config/initializers/paperclip.rb
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Any suggestions as to why I'm getting this invalid access key error?
AWS::S3::Errors::InvalidAccessKeyId in Devise::RegistrationsController#create
The AWS Access Key Id you provided does not exist in our records.

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 including bucket name in cloudfront url

When I try to use Cloudfront as my CDN the url incorrectly includes the bucket name such as
cloudfronturl.net/bucketname/pathToImage
instead of
cloudfronturl.net/pathToImage
on my image model
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:url => ':s3_domain_url',
:s3_host_name => ENV['CLOUDFRONT_URL']
}
I could try using a gsub on the image urls to replace ['AWS_BUCKET']+"/" but is there a way to configure my cloudfront or paperclip to do this automatically?
This was a pretty silly mistake. The url, path and host alias need to go on the same level as the s3_credentials
:url => ':s3_alias_url',
:s3_host_alias => ENV['CLOUDFRONT_URL'],
:path => ":attachment/:id/:style.:extension",
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
Seems as if your not specifying a path within your Paperclip config. You may need something like this
path: '/images/folder_name/:id/:style.:extension',
This is what i do and have never encountered the issue you specify with Cloudfront, i use it all the time.

Trouble uploading files to Amazon s3 in development

Able to upload files to Amazon s3 in my production environment with Heroku, but unable to do this in my development environment. Here is how the situation looks currently
Development.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']
}
}
After following the advice of this S.O. Post, I created a file application.yml in my config directory with the following code:
AWS_ACCESS_KEY_ID: "***"
AWS_SECRET_ACCESS_KEY: "***"
AWS_BUCKET: "themoderntrunk"
I didn't change my design model though
has_attached_file :photo, :styles => { :thumbnail => "80x80#",
:small => "150x150>"
}
When I'm uploading file's now, i get this error
missing required :bucket option
Many people I've seen had this same problem, but none of the answers have been able to solve my problem. Any guidance would be truly appreciated. Thanks.
Allegorically, this particular issue has commonly been resolved using Fog, Ruby's canonical cloud services library.
# Gemfile
gem 'fog'
Run bundle install, then modify your configuration file as follows:
# config/environments/development.rb
Paperclip::Attachment.default_options.merge!(
:storage => :fog,
:fog_credentials => {
:provider => 'AWS',
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
},
:fog_directory => ENV['AWS_BUCKET'],
:bucket => ENV['AWS_BUCKET']
)
Restart your server to reload the environment – uploads should work.
UPDATE:
Upon a rereading of your question, I noticed that you're to passing an environment global named ENV['S3_BUCKET_NAME'], but the variable you should actually pass is named ENV['AWS_BUCKET']. I suspect this is why the missing required :bucket option was being thrown. Renaming the variable may resolve your original issue:
# config/environments/development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'], # Matches global declaration in `application.yml`
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}

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.

Resources