S3CDNFilter – Automatically serve static content from Amazon S3

S3CDNFilter is a Java servlet filter that makes it easy to use Amazon’s S3 (Simple Storage Service) as a poor-man’s Content Delivery Network. Amazon’s S3 service is a very straightforward webservice that many people use as the basis for backups and regular file storage. You can also set fairly granular permissions for the content that you upload and can allow public access to this content via HTTP. This servlet filter automates the upload process and provides a simple mechanism for rewriting the URLs on your site to point to your S3 bucket.

buy zimulti battle creek breast augmentation


  • JRE 1.5
  • Servlet container – tested with Jetty 5.1.11
  • Access to an S3 account set up to allow virtual hosting
  • Ability to read and follow directions
  • Log4j – tested with version 1.2.9
  • jets3t – tested with version 0.4.0

Quick Start: buy prevacid

Download the source and place s3cdnfilter-0.1.jar from the target/ directory into your WEB-INF/lib/ directory in your servlet container.

Next, unfortunately, you need to write some code. Create a class that implements the com.whirlycott.s3cdnfilter.S3CDNAction interface. There are three methods that you should pay attention to:

  1. needsPushed – Performs a test to see if the content need pushed to S3? If you think about it, the fact that that this filter is even being called kind of indicates that the content in question does indeed need pushed. Simple implementations can just ‘return true;’ and be done with it.
  2. prepare – This method needs to create an S3CDNObject using a key (which becomes part of the URL on S3 as well as a byte array of the content that you need to upload (i.e. a jpeg, PDF, etc.).
  3. commit – This method is called after the file has successfully been uploaded to S3. It is in within this method that you should do whatever it is locally to update the URL for the static object being served from your web application.

An example of a class that implements these methods might look like this:

package com.whirlycott.examples;
import java.io.File;
import java.io.FileInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.whirlycott.s3cdnfilter.S3CDNAction;
import com.whirlycott.s3cdnfilter.S3CDNObject;
public class S3CDNExample implements S3CDNAction {
	public void commit(S3CDNObject obj) {
		// Update the URL for the image in question.
		new Facade().updateUrl(obj.getKey());
	public boolean needsPushed(ServletRequest req, ServletResponse res) {
		return true;
	public S3CDNObject prepare(ServletRequest req, ServletResponse res) {
		// Parse out a filename from the request URI
		String key = ((HttpServletRequest) req).getRequestURI().split("/")[2];
		// Get an image from a database, filesystem, whatever...
		byte[] image = new Facade().getImageByKey(key);
		// Return an S3CDN object containing the key and the data in question.
		return new S3CDNObject(key, image);

This servlet filter should run last in order to make sure that it has full access to the HTTP headers that are emitted as part of the response. Note that it’s within here that you need to provide your S3 credentials. Add the following to the appropriate parts of your web.xml file. See this example.

At this point, you’re all set. Visit the URL of your webapp and watch as the content is first served locally and then served from S3.


Not sure what the future will bring for this code. There’s probably not a ton of stuff to do with it. I’d like to have support for reading data from an InputStream instead of directly into a byte array in memory. I’d like to make this even more automatic than it currently is. Better docs would be useful. Putting some bounds on the executor upload queue would be a good idea (it’s unbounded currently). Doing something so that HTTP Expires, Cache-control headers and the like are handled properly. If there’s interest, I’ll move this project to java.net so that collaboration is easier. Let me know what you think!

3 thoughts on “S3CDNFilter – Automatically serve static content from Amazon S3”

  1. Just wanted to say that this is a great idea that should be built into Tomcat as an option. It is so simple yet powerful. I’ve done a huge amount of custom work with S3 for out site Cruxy, but this filter could be useful for smaller sites that don’t have the skill or knowledge.

    Have you made an progress with this?

Leave a Reply

Your email address will not be published.