I have just set up file uploads to Amazon S3 using Rails 3 and Paperclip. All this works amazingly well and is up and running. There is just one small detail that I would like to sort out. At the moment, the urls are the amazon urls (ie start http://s3.amazonaws.com) and I would like them to begin with my domain.
I have already added the neccesary CNAME records to my DNS and they are working fine so I can access the files via a subdomain of my domain. The problem is just that the URLs generated by paperclip start with the amazon domain. Is there an easy way to change the paperclip config to get round this?
Cheers
Take a look at Paperclip::Storage::S3
.
Here's everything you need to hide the Amazon urls of your S3 assets:
Name your S3 bucket after the domain alias you want to use. So if you want to access your assets at http://assets.mysite.com/path/to/image.png
then you should name your S3 bucket: assets.mysite.com
Add a CNAME to your DNS records so that assets.mysite.com
is an alias of assets.mysite.com.s3.amazonaws.com
(Don't include '.mysite.com' in 'name' field of the DNS record.)
Setup paperclip to use your new domain alias insetad of the default S3 path:
has_attached_file :my_file,
...
:url => ':s3_alias_url'
:s3_host_alias => 'assets.mysite.com',
...
I usually have different buckets for development, staging and production and I only use the domain alias for the prod bucket. So to ensure it works in each environment, my :url
setting often like this:
:url => (ENV['USE_S3_ALIAS'] == 'TRUE' ? ':s3_alias_url' : ':s3_domain_url')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With