Updated 10/26/09: added improved url rewriting rules
So you want an easy to setup CDN that does not require much work and integrates with Drupal. Here is a quick setup to get to serve assets automatically from your Drupal files directory from S3
What you need:
- Drupal site
- Media Mover
- S3 account
Get started
Install Media Mover, enable the S3, Directory, and AutoRun modules on your admin/build/modules page. Go to admin/settings/media_mover and enter your S3 credentials.
Now you need to create a new Media Mover configuration. Go to admin/build/media_mover/add and create a new configuration. You will only need to elements for this configuration- the Media Mover directory and S3 modules.
First you need to define where Media Mover will harvest its files from. For this example, we are going to move media files from the drupal files directory. Note that we could chose to move CSS and JS files as well- if you want to do this, remember two things: you must run this Media Mover configuration on cron so that any new CSS or JS files generated by aggregation will be moved over and you will need to make sure that your URL rewrite rules match your choices. You also will have to make sure that JS and CSS files that are being aggregated are generated and then moved to Amazon. This is a bit more complicated- you are going to need some knowledge about handling URLs with Apache to do this.
Now you need to set your configuration options for S3:
This is the configuration that will move your files to Amazon. One other item that we want to make sure is that this configuration is setup to run every time a user uploads content. Head back to: admin/settings/media_mover and enable AutoRun for your configuration.
Now we can run the configuration to move all your files up to Amazon. Depending on the size of your directory, this can take a LONG time. You may want to adjust the number of files handled by this configuration under the advanced setting. Head back over to admin/build/media_mover and run your configuration. Once this is complete you will have your files directory mirrored on Amazon. Cron and AutoRun will help keep your directories in sync, but keep in mind the following: there can be a time delay between files on your local file system making it to amazon. Two, files on Amazon are not deleted when files in your directory are deleted.
The final step that you need to do is to is add a rewrite rule in your .htaccess file to redirect your files directory to Amazon. These lines should go at the bottom of the rewriting rules in the file. You may want to be sneaky and use a subdomain to access your site without the CDN (usefull for editing and other content operations), however with cron running frequently this should not be a huge issue.
# S3 REWRITE RULES
# Only do the rewrite if the file exist locally- this helps us generate imagecache files
RewriteCond %{DOCUMENT_ROOT}/sites/default/files/$1 f
RewriteRule .*sites\/default\/files\/(.*[jpg|png|gif|flv|mov])$ http://your.amazon.url/sites/default/files/$1 [L,QSA]
These rules allow apache to try to serve a local file if it does not exist. This is important for imagecache as the "physical" file does not exist until a user goes to its URL. The main drawback about this system is the lag between files being uploaded and files getting to amazon.
Note that I am specifying what kinds of files to use here so that the JS and CSS files in my files directories are not disturbed. Also note that this does not work with Drupal's private files.




Amazon S3 freeware client
I always enjoy learning what other people think about Amazon Web Services and how they use them. Check out my very own tool CloudBerry Explorer that helps to manage S3 on Windows . It is a freeware. http://cloudberrylab.com/
Pretty cool!
Thanks for pointing me to this blog post. That's pretty cool! :)
I forgot to mention this in
I forgot to mention this in my first comment: your rewrite rule will fail when the file has not yet been synced to S3!
Yeah, needs to be updated
You're right- this needs to be updated! There is also the issue of preview images as well- ultimately there has to be a rule that does not rewrite the tmp directory.