Paperclip with Amazon S3

Dan Barber edited this page Jul 6, 2016 · 15 revisions

You can use Paperclip for saving files to Amazon S3.

see also https://devcenter.heroku.com/articles/paperclip-s3

http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3

Use gems “aws-sdk” and “paperclip”

In development.rb:

config.paperclip_defaults = {
  :storage => :s3,
  :s3_host_name => 'REMOVE_THIS_LINE_IF_UNNECESSARY',
  :bucket => 'S3_BUCKET_NAME'
}

In production.rb:

config.paperclip_defaults = {
  :storage => :s3,
  :s3_host_name => 'REMOVE_THIS_LINE_IF_UNNECESSARY',
  :bucket => 'S3_BUCKET_NAME'
}

In aws.yml (automatically loaded by aws-sdk):

development:
  access_key_id: AWS_ACCESS_KEY_ID
  secret_access_key: AWS_SECRET_KEY_ID

production:
  access_key_id: AWS_ACCESS_KEY_ID
  secret_access_key: AWS_SECRET_KEY_ID

You may need to state the s3 host_name if other than US standard:
http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3#s3_protocol-instance_method

AWS Permissions

The access key and secret can be obtained from AWS when you create an IAM user. You will need to add a policy to the bucket as well, so the user has appropriate permissions. Here is a template you can use:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
      "AWS": "(user-ARN)"
    },
      "Action": "s3:*",
      "Resource": "arn:aws:s3:::(bucket-name)/*"
    }
  ]
}

The users ARN can be obtained from the IAM console.