15

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.

huoxito
  • 560
  • 6
  • 7

3 Answers3

14

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'
)
dom
  • 11,894
  • 10
  • 51
  • 74
  • 1
    I tried that. But it returns this error Paperclip::Errors::InfiniteInterpolationError The docs also makes me think that it would be the solution http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3. But it's not working. – huoxito Jun 21 '12 at 03:32
  • 24
    You must also define the `:path` option with `:url`, because the default one includes `":url"` that causes the `Paperclip::Errors::InfiniteInterpolationError` exception. Paperclip bug automatically fixes the `:path` option for S3 storage... unless you use a :s3_*_url in the interpolation of `:url` (yes this is a bug). Use this for instance: `"/:class/:attachment/:id_partition/:style/:filename'"`. – Julien Portalier Jul 30 '12 at 15:09
  • 2
    @JulienPortalier there's an erroneous single-quote at the end your example that threw me off for a couple hours. The correct setting is: `:path => "/:class/:attachment/:id_partition/:style/:filename"` – Sky Aug 05 '14 at 01:03
4

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
}
Sagar Ranglani
  • 5,491
  • 4
  • 34
  • 47
1

If you bucket name is DNS compatible then you can create url .s3.amazonaws.com/object....

but if it is not DNS compatible then you can not create as you want.

Thanks

Tej Kiran
  • 2,218
  • 5
  • 21
  • 42
  • Actually I can see the uploaded image if I directly access the url with bucket.s3.amazonaws.com/the-path-to-image I just can't make paperclip generate urls this way. – huoxito Jun 21 '12 at 03:40