views:

24

answers:

1

I'm trying to figure out if I can get browsers to cache images with signed urls.

What I want is to generate a new signed url for every request (same image, but with an updated signature), but have the browser not re-download it every time.

So, assuming the cache-related headers are set correctly, and all of the URL is the same except for the query string, is there any way to make the browser cache it?

The urls would look something like:

http://example.s3.amazonaws.com/magic.jpg?WSAccessKeyId=stuff&Signature=stuff&Expires=1276297463
http://example.s3.amazonaws.com/magic.jpg?WSAccessKeyId=stuff&Signature=stuff&Expires=1276297500

We plan to set the e-tags to be an md5sum, so will it at least figure out it's the same image at that point?

My other option is to keep track of when last gave out a url, then start giving out new ones slightly before the old ones expire, but I'd prefer not to deal with session info.

+1  A: 

The browser will use the entire URL for caching purposes, including request parameters. So if you change a request parameter it will effectively be a new "key" in the cache and will always download a new copy of that image. This is a popular technique in the ad-serving world - you add a random number (or the current timestamp) to the end of the URL as a parameter to ensure the browser always goes back to the server to make a new request.

The only way you might get this to work is if you can make the URL static - i.e. by using Apache rewrite rules or a proxy of some sort.

Marc Novakowski
Do you know if it will at least check the e-tag and not download it again?
Brendan Long
The ETag header is only sent by the browser to the server when it finds the file in it's cache (and if the original file had an ETag value). So if it can't find it in its cache (which it won't if the URL has changed), there's no ETag value to send in the request.
Marc Novakowski
The URL rewrite rule would then again make the S3 signature useless, because the web server would automatically provide a not expired link. Then you could also mark files in S3 as "public". (Then, no signature is required to access the files)
Tarnschaf