Railo and Amazon S3

I don’t usually get the time to play around with Amazon’s cloud offerings, but I’ve started a project recently which has a need for CloudFront, Amazon’s Flash Media Server streaming solution.

I remembered that Railo 3.x and upwards has S3 support (which CloudFront uses as it’s storage) – I wasn’t really prepared for quite *how* easy this was to implement. As a test, all I wanted to do was build a list of files on a S3 bucket, and then construct some URLs for an MP3 playlist in flowplayer.

Firstly, you need to get your Amazon access keys (which I’ll assume you know how to do) via the AWS console.

Next, add this to your onApplicationStart() method (or on cfWheels, your events/onApplicationStart.cfm):

<cfscript>
application.s3.accessKeyId = "MYACCESSKEY";
application.s3.awsSecretKey = "MYSECRETKEY";
application.s3.host="s3.amazonaws.com";
</cfscript>

To get all the files in my S3 bucket, I can then simply do this:

<cfscript>
S3Key=application.s3.accessKeyID;
S3Sec=application.s3.awsSecretKey;
</cfscript>

<cfdirectory action="list"
directory="s3://#S3Key#:#S3Sec#@s3.amazonaws.com/BUCKETNAME/FOLDERNAME" name="s3Directory"
recurse="yes" type="file">

Then, for flowplayer, I needed that data to be looped out into a JS friendly format for the playlist (RTMP streaming uses the mp3: prefix, and I also need to strip out the secret/access key from the returned directory path)

<cfloop query="s3Directory">
<cfoutput>
{url: "mp3:#replace(directory, "s3://#S3Key#:#S3Sec#@/BUCKETNAME/", "", "all")#/#replace(name, '.mp3', '', 'all')#"}<cfif s3Directory.recordcount NEQ currentrow>,</cfif>
</cfoutput>
</cfloop>
</cfsavecontent>

Then in my flowplayer config, I can just add:

playlist: [#playlist#]

… as an option.

Nice and simple, and made v easy by Railo – thanks guys!