Permission denied Paperclip and s3 error - ruby-on-rails

I have a rails 4 app deployed on heroku. I'm using Amazon s3 bucket to host images (using paperclip gem), but when I try to upload an image to my articles controller i get this error:
Permission denied # dir_s_mkdir - /articles
I have gone through the steps listed here. As i understand this could be a folder permission issue, but not really to sure how to tackle the issue.

Try putting the following code in your /config/environments/production.rb file and update ENV secret key names accordingly. The host name is dependent on your region, us-west-1, eu-west-1, etc.
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'],
:s3_host_name => 's3-us-west-1.amazonaws.com'
}
}

Related

Rails Paperclip adds extra folder pointing to Cloudfront (between :url and :path)

I'm having trouble with loading images uploaded to s3 via my cloudfront distribution. I can get it to work fine loading straight from s3, but not from cloudfront.
I have set the config.action_controller.asset_host to my distribution url and have set up paperclip
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-sa-east-1.amazonaws.com',
:url => ':asset_host',
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
with the appropriate code in the model :path =>
':class/:id/:style/:class:style:id.:extension'
and every time I upload a file and try to access it i get
https://XXXXXX.cloudfront.net/images/:class/:id/:style/:class:style:id.:extension
I honestly can't figure out where the /images/ is coming from that is added between the url and the path. And I would be very greatful for any help or tips to solve this!
Thanks

set up paperclip with amazon s3

I am trying to set up the paperclip with amazon s3, I have found a tutorial which is like the following code. What do I have to put as a url and path ?
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']
},
:url => ':s3_domain_url',
:path => "/:class/:attachment/:id_partition/:style/:filename"
}
You don't have to worry about touching url, unless you care about whether or not the URL is placed domain-style (bucket-name.s3.amazonaws) or path-style (s3.amazonaws.com/bucket-name). You could not add that in your configurations and it will default to domain-style.
Path tells Paperclip what path to save your attachments. This means in local file system, it will save it under "(model's name)/(attachment name)/(id)/(style e.g. small, thumbnail)/(file name)." It will do the same thing for your bucket, so you'll have all these fun folders to click through if you view your physical bucket. You're free to leave it to the default, or change it if you want your path to be a little shorter.

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.

RoR AWS-SDK Error when accessing bucket in Europe region

I am using latest aws-sdk for Ruby. My rake file contains following line to connect to S3 bucket:
require 'aws-sdk'
...
AWS.config(
:access_key_id => 'xxxxx',
:secret_access_key => 'xxxxx'
)
s3 = AWS::S3.new
object = s3.buckets['bucket_in_ireland'].objects['data.csv']
When accessing an AWS S3 bucket in region US that works perfectly fine. But getting following error while accessing AWS S3 bucket in region Ireland:
AWS::S3::Errorrs::PermanentRedirect: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
From Google I could find two solutions:
Put AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com" at the bottom of environment.rb
Set :s3_host_name => 's3-eu-west-1.amazonaws.com' for paperclip default settings
When I placed the line mentioned in (1) at the bottom of environment.rb, the error message changed to following:
NameError: uninitialized constant AWS::S3::DEFAULT_HOST
Then I tried (2) by updating following code in production.rb but that didn't help.
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'],
:s3_host_name => 's3-eu-west-1.amazonaws.com'
}
}
Note: Also tried to apply this fix [https://github.com/aws/aws-sdk-ruby/issues/510] but nothing really changed after modifications in environment.rb, but nothing work.
Any help please...
Setting s3_endpoint to AWS config solved the issue for me.
AWS.config(
:access_key_id => 'xxxx',
:secret_access_key => 'xxxx',
:s3_endpoint => 's3-eu-west-1.amazonaws.com'
)

Paperclip/heroku/S3 Configuration is setup as best as I can tell but getting error on upload

Here's the error I get when I try to upload an image through the live site:
ArgumentError (missing required :bucket option):
app/controllers/editions_controller.rb:53:in `block in create'
app/controllers/editions_controller.rb:52:in `create'
I have the latest versions of aws-sdk and paperclip
I've got the following in production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['bucket_name'],
:access_key_id => ENV['key'],
:secret_access_key => ENV['key2']
}
I've got my AWS_BUCKET set on my heroku app:
AWS_BUCKET: bucket_name
S3_BUCKET_NAME: bucket_name
AWS_ACCESS_KEY_ID: key
AWS_SECRET_ACCESS_KEY: key2
Can anybody suggest why this isn't working? Thanks.
You've got:
:s3_credentials => {
:bucket => ENV['bucket_name']
}
I've always been told that you should capitalize the constants like so:
:s3_credentials => {
:bucket => ENV['BUCKET_NAME']
}
Additionally, you have not actually set an ENV variable called BUCKET_NAME on heroku, you have the following:
AWS_BUCKET: bucket_name
But if your env var is called BUCKET_NAME, then you need the following instead:
BUCKET_NAME: bucket_name
So from the commane line you'd want to run something like heroku config:set BUCKET_NAME=bucket_name
And finally, you've changed the names of your env vars from those that are suggested in the paperclip readme. You've gone with ENV['BUCKET_NAME'] when everyone else uses ENV['AWS_BUCKET']. I dunno if that will effect anything, but try using the process outlined here by heroku.
Try those changes and let me know how they work out. Good luck.

Resources