Amazon S3 cache audio files
amazon-s3, amazon-web-services, caching, ruby-on-rails
Solution
If you're using signed URLs, which you say you are in the comments, and not reusing those signed URLs then there is no way to cache these requests.
Amazon Web Services cannot override your Web browser's internal cache system. When two URIs are unique, as they are with signed URLs, then your Web browser treats them as two distinct resources on the Internet.
For example, let's take:
http://www.example.com/song1.mp3
http://www.example.com/song2.mp3
These are two discrete URIs. Even if `song1.mp3` and `song2.mp3` had the same `Etag` and `Content-Length` HTTP response headers, they're still two different resources.
The same is true if we merely alter their query strings:
http://www.example.com/song1.mp3?a=1&b=2&c=3
http://www.example.com/song1.mp3?a=1&b=2&c=4
These are still two discrete URIs. They will not reference one another for purposes of caching. This is the principle behind using query strings to override caching.
No amount of fiddling with HTTP headers will ever get you the cache behavior you're seeking.
Problem
I have created new music application and I store all mp3 files on Amazon S3. Before moving to S3 I used store them on server file system itself. It used to cache files and on consecutive reload of page files weren't downloaded from server. But after moving to S3 everytime I load page it downloads files from S3. This not only making my app slow but every request to S3 is money. I found some documentation on cache-control and I tried them all but no success. I might be missing something here. Any help is appreciated. Thanks. Here is my code for uploading mp3 files on S3. I use CarrierWave with Rails. ``` CarrierWave.configure do |config| config.fog_credentials = { :provider => 'AWS', :aws_access_key_id => MyAppConfig.config['aws']['aws_access_key'], :aws_secret_access_key => MyAppConfig.config['aws']['aws_secret_key'], } config.fog_directory = MyAppConfig.config['aws']['aws_bucket_name'] config.fog_public = false config.storage = :fog config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} end ```