Directly Uploading a File to Amazon S3 with Railo

On November 29, 2011, in AWS, Railo, by Anuj Gakhar

I work with Railo and Amazon S3 on an almost daily basis and one of the things I recently looked at was how to upload a file directly to Amazon S3 when a form with a file type field is submitted. Turns out it is pretty easy to do so.  Railo has built in support for Amazon S3 and to set it up, you have to define your AWS credentials in your Application.cfc, like this :-

[cf]this.s3.accessKeyId = "ACCESS_KEY";
this.s3.awsSecretKey = "SECRET_KEY";[/cf]

Once that’s done, you can start using S3 buckets pretty much the same way as you do with other File and Directory functions.

To upload a file, you would simply do something like this :-
[cf]<cffile action=="upload" destination="s3://YOUR_BUCKET/foldername/" fileField = "filedata"
nameConflict = "MakeUnique" acl = "public-read"/>[/cf]

The above code will take the data from the input field “filedata” and upload directly to the S3 bucket of your choice. However, one issue that I have noticed is (correct me if I am wrong here) that the files that have spaces in their names do not get uploaded to S3 at all. The CFFILE attributes like CFFILE.serverfile etc will all get returned correctly (to make matters worse) but the actual file never gets written to the S3 folder. I can say that this only happens with files with spaces or special characters in them. So, I came up with this little example here that use some Javascript to append the original filename to a hidden form field (since Railo will not give you the real filename before upload) which you can then use to fix the filename before uploading to S3. All I am doing here is, taking the original filename, removing any spaces and special characters from the name and then using that name in the S3 upload call.

Here is the complete example :-

<script src="//"></script>
<script type="text/javascript">
<form method="post" enctype="multipart/form-data">
<input name="filedata" type="file" value="Browse" id="fileName" />
<input type="hidden" name="actualFileName" value="" id="actualFileName"/>
<input type="submit" name="submit" value="Upload"/>
<cfif structKeyExists(form, "submit")>
<cfset s3Path = "s3://YOUR_BUCKET_NAME/testuploads/" />
<cfif not DirectoryExists(s3Path)>
<cfset DirectoryCreate(s3Path, true) />
<cfset realFileName = GetFileFromPath(actualFileName) />
<cfset realFileExt = ListLast(realFileName,".") />
<cfset realFileNameWithoutExt = rereplacenocase(realFileName, ‘\.[^.]+$’ , ” )/>
<cfset newFileName = sluggify(realFileNameWithoutExt) & "." & realFileExt />
<cfset upl = FileUpload(destination = s3path & newFileName, fileField = "filedata",
nameConflict = "MakeUnique",acl = "public-read") />

The code uses the function “sluggify”, which I have not included in the code but can be found here.

Also, worth noting is that I am using Railo final for this demo. If anyone has a better solution for fixing the filename issue before uploading, let me know.

Tagged with:  

One Response to Directly Uploading a File to Amazon S3 with Railo

  1. Jeromy says:

    Something else I was looking for! Thanks Anuj.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar