On Amazon S3 you can access your files through different URL’s, depending on which suites best your requirements:
- Your bucket as “folder”: http://s3.amazonaws.com/your-bucket/your-file.txt
- As subdomain: http://your-bucket.s3.amazonaws.com/your-file.txt
- With a CNAME on your own domain: http://images.my-domain.com/your-file.jpg
How to setup a CNAME with Amazon S3 is explained pretty well here.
I had however some trouble setting up the correct URL to this CNAME in Paperclip, the popular Rails gem for handling file uploads.
This is the final configuration which is working for me is:
has_attached_file :image, :storage => 's3', :s3_credentials => { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'] }, :s3_headers => { 'Expires' => 1.year.from_now.httpdate }, :s3_host_alias => 'subdomain.my-domain.com', :bucket => 'subdomain.my-domain.com', :url => ":s3_alias_url", :path => "/images/:class/:id_:basename.:style.:extension", :styles => { :thumb => '70x70#', :medium => '350x350', :original => '1000x1000>' }, :default_style => :original, :convert_options => { :all => '-strip -trim' } |
I’m using Heroku as hosting service, therefore I save the S3 key and secret in an environment variable.
:s3_host_alias is the CNAME which you need to configure for your domain. The exact same string needs to be as well the name of the bucket.
Finally just assign “:s3_alias_url” to :url. Without that the URL won’t be generated correctly. You would end up with something like “http://s3.amazonaws.com/your-file.txt” which is missing entirely the bucket name and would not load the file.
Note that for this configuration it’s not necessary to define :bucket.

I'm a Manager in R&D, currently working at