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 :-

this.s3.accessKeyId = "ACCESS_KEY";
this.s3.awsSecretKey = "SECRET_KEY";

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 :-

<cffile action=="upload" destination="s3://YOUR_BUCKET/foldername/" fileField = "filedata" 
nameConflict = "MakeUnique" acl = "public-read"/>

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 :-

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$('#fileName').change(function(){
			$('#actualFileName').val($(this).val());
		})
	})	
</script>	
</head>
<body>				
<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"/>
</form>
<cfif structKeyExists(form, "submit")>
	<cfset s3Path = "s3://YOUR_BUCKET_NAME/testuploads/" />
	<cfif not DirectoryExists(s3Path)>
		<cfset DirectoryCreate(s3Path, true) />
	</cfif>
	<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") />
</cfif>	
</body>
</html>

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 3.3.1.004 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.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 446 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: