Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple S3 buckets with carrierwave gem

I have just recently setup my rails 3.2 app to use the carrierwave gem and upload files to S3. What I don't see is the ability to use a different bucket per uploader. Does anyone know if this is a possiblity?

like image 530
kennethlmartin Avatar asked Mar 11 '12 21:03

kennethlmartin


People also ask

Can you have multiple s3 buckets?

There is no max bucket size or limit to the number of objects that you can store in a bucket. You can store all of your objects in a single bucket, or you can organize them across several buckets. However, you can't create a bucket from within another bucket.

Why have multiple s3 buckets?

Simpler Permission with Multiple Buckets If the images are used in different use cases, using multiple buckets will simplify the permissions model, since you can give clients/users bucket level permissions instead of directory level permissions.

What is carrierwave gem?

This gem provides a simple and extremely flexible way to upload files from Ruby applications. It works well with Rack based web applications, such as Ruby on Rails.


2 Answers

The bucket is specified via the fog_directory config. This configuration option is defined on the uploader and could simply be overwritten with your own method.

Just add the following to your uploader:

def fog_directory
  # your bucket name here
end
like image 127
toashd Avatar answered Sep 21 '22 13:09

toashd


The carrierwave wiki explains how to use a separate s3 bucket for each uploader:

def initialize(*)
  super

  self.fog_credentials = {
    :provider               => 'AWS',              # required
    :aws_access_key_id      => 'YOURAWSKEYID',     # required
    :aws_secret_access_key  => 'YOURAWSSECRET',    # required
  }
  self.fog_directory = "YOURBUCKET"
end
like image 23
Eero Avatar answered Sep 22 '22 13:09

Eero