Callmeed
Callmeed

Reputation: 4992

How to use multiple S3 accounts with Paperclip and Rails

I'm fairly familiar with using Paperclip in my Rails apps and having attachments stored on S3.

However, on my next project I'd like each user to use their own S3 account. Assuming I'm using restful_authentication or authlogic, what would be the best way to store each user's credentials and have Paperclip use them?

Upvotes: 4

Views: 867

Answers (2)

Lukas Eklund
Lukas Eklund

Reputation: 6138

I had the same issue, so I wrote up a rudimentary plugin that will extend Paperclip to allow for multiple S3 accounts. It lets you pass a Proc for the s3_credentials option rather than just a hash or a file. The Proc has to return a hash or a file.

https://github.com/leklund/paperclip_multiple_s3_accounts

All it does is add one line to parse_credentials:

creds = creds.is_a?(Proc) ? creds.call(self) : creds

and then you can do something like (as long as your model belongs_to :s3bucket):

has_attached_file :s3_credentials => (lambda do |attachment|
    h = {
    'bucket'            => attachment.instance.s3bucket.name,
    'access_key_id'     => attachment.instance.s3bucket.access_key_id,
    'secret_access_key' => attachment.instance.s3bucket.secret_access_key
    }
 end)

Upvotes: 1

rordude
rordude

Reputation: 81

Why not just use one S3 account and create a folder(bucket) for each user.

Upvotes: 0

Related Questions