Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up bucket's name placed domain-style (bucket.s3.amazonaws.com) with Rails and Paperclip

Paperclip doc about url options:

You can choose to have the bucket's name placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket).

How would look like the setup to actually have bucket's name placed domain-style? I can't force paperclip to generate urls like bucket.s3.amazonaws.com instead of s3.amazonaws.com/bucket.

like image 332
huoxito Avatar asked Jun 19 '12 05:06

huoxito


People also ask

What are the two styles of urls that AWS supports for S3 bucket access?

Currently, Amazon S3 supports both virtual-hosted–style and path-style URL access in all AWS Regions.

How set S3 bucket name?

The following rules apply for naming buckets in Amazon S3: Bucket names must be between 3 (min) and 63 (max) characters long. Bucket names can consist only of lowercase letters, numbers, dots (.), and hyphens (-). Bucket names must begin and end with a letter or number.

How do I mask my Amazon S3 URL?

There are two parts to masking your S3 url, the first is creating and naming a bucket in S3 to match the subdomain, the second is creating the subdomain and pointing it to the S3 bucket url.

Can we create S3 bucket with same name in different region?

Create a new bucket in another region. Note that you can not have the same bucket name as your current one because bucket names must be unique. Copy the contents of the current bucket to the new bucket created in the region you prefer. Once copied, delete the old bucket.


2 Answers

Just set it like this:

Paperclip::Attachment.default_options[:url] = ':s3_domain_url'

Or like this:

Paperclip::Attachment.default_options.merge!(
  :url => ':s3_domain_url'
)
like image 137
dom Avatar answered Oct 14 '22 00:10

dom


Add :url and :path to the Paperclip default options in your application.rb or environment.rb

config.paperclip_defaults = {
  storage: :s3,
  s3_credentials: {
    bucket: ENV['MY_S3_BUCKET_NAME'],
    access_key_id: ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
  },
  url: ':s3_domain_url',                                     # ADD THIS
  path: '/:class/:attachment/:id_partition/:style/:filename' # ADD THIS
}
like image 25
Sagar Ranglani Avatar answered Oct 14 '22 00:10

Sagar Ranglani