I'm following Hartl's railstutorial.org and have arrived at 11.4.4: Image upload in production. What I've done:
- Signed up for Amazon Web Services
- In Amazon Identity and Access Management I created a user. The user was created succesfully.
- In Amazon S3 I created a new bucket.
- Set permissions on the new bucket:
Permissions: The tutorial instructs to "grant read and write permission to the user created in the previous step". However, under 'Permissons' for the bucket the new user's name is not mentioned. I could only choose between Everyone, Authenticated users, Log delivery, Me, and a username Amazon seems to have created itself from my name + a number. I have tried it by selecting Authenticated users and checked the boxes for Upload/Delete and View permissions (and not selecting List and Edit permissions). I also tried it by selecting Everyone and checked all the four boxes. The results were the same.
- I implemented in my Rails application following the instructions of the tutorial (I don't belief anything wen't wrong there, so I haven't included those steps here to not make it too long).
The initializer carrier_wave.rb
is set to the code below. I've added region: 'eu-west-1'
to the initializer (an idea I got from here) to get rid of the message connecting to the matching region will be more performant
.
if Rails.env.production? CarrierWave.configure do |config| config.fog_credentials = { # Configuration for Amazon S3 :provider => 'AWS', :aws_access_key_id => ENV['S3_ACCESS_KEY'], # Set these key's using heroku config:set S3_ACCESS_KEY=<access key> :aws_secret_access_key => ENV['S3_SECRET_KEY'], :region => 'eu-west-1' } config.fog_directory = ENV['S3_BUCKET'] end end
- It worked in development and I pushed to Heroku.
Error: When using the uploader in production to upload an image, I get the error We're sorry, but something went wrong.
The Heroku server log says:
What have I done wrong? I seems to have something to do with the permissions?
Update: The cause turned out to be the policies granted to the user. If I grant the user standard AmazonS3FullAccess then it works. It doesn't work with just AmazonS3ReadOnlyAccess, since then the user can't save a new image. In my application the user basically only needs 2 rights: upload its own avatar image and read the avatar image. Would it be safe to use AmazonS3FullAccess or should I write my own custom policy?
I tried the custom policy below, which should give the application read and write rights (adopted from here) but that still generated the 403 Forbidden
error.
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": ["s3:ListBucket"], "Resource": ["arn:aws:s3:::mybucket"] }, { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:GetObject", "s3:DeleteObject" ], "Resource": ["arn:aws:s3:::mybucket/*"] } ] }