Created
January 28, 2013 19:11
-
-
Save atuttle/4658165 to your computer and use it in GitHub Desktop.
Upload files to Amazon S3 with ColdFusion. This is heavily based on code from Joe Danziger's s3.cfc: http://amazons3.riaforge.org/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="uploadToAmazonS3"> | |
<cfargument name="fileName" required="true" /> | |
<cfargument name="contentType" required="true" /> | |
<cfargument name="data" required="true" /> | |
<cfargument name="acl" default="public-read" /> | |
<cfargument name="storageClass" default="STANDARD" /> | |
<cfargument name="HTTPtimeout" default="300" /> | |
<cfargument name="bucket" default="#getProperty('EmailAttachmentS3Bucket')#" /> | |
<cfargument name="accessKeyId" default="#getProperty('EmailAttachmentS3AccessKeyId')#" /> | |
<cfargument name="secretKey" default="#getProperty('EmailAttachmentS3SecretKey')#" /> | |
<cfset var dateTimeString = GetHTTPTimeString(Now())> | |
<!--- authorization stuff ---> | |
<cfset var cs = "PUT\n\n#arguments.contentType#\n#dateTimeString#\nx-amz-acl:#arguments.acl#\nx-amz-storage-class:#arguments.storageClass#\n/#arguments.bucket#/#arguments.fileName#"> | |
<cfset var signature = createSignature(cs, arguments.secretKey)> | |
<cfset var url = "http://s3.amazonaws.com/#arguments.bucket#/#arguments.fileName#" /> | |
<cfhttp method="PUT" url="#url#" timeout="#arguments.HTTPtimeout#"> | |
<cfhttpparam type="header" name="Authorization" value="AWS #arguments.accessKeyId#:#signature#"> | |
<cfhttpparam type="header" name="Content-Type" value="#arguments.contentType#"> | |
<cfhttpparam type="header" name="Date" value="#dateTimeString#"> | |
<cfhttpparam type="header" name="x-amz-acl" value="#arguments.acl#"> | |
<cfhttpparam type="header" name="x-amz-storage-class" value="#arguments.storageClass#"> | |
<cfhttpparam type="body" value="#arguments.data#"> | |
</cfhttp> | |
</cffunction> | |
<cffunction name="HMAC_SHA1" returntype="binary" access="private" output="false" hint="NSA SHA-1 Algorithm"> | |
<cfargument name="signKey" type="string" required="true" /> | |
<cfargument name="signMessage" type="string" required="true" /> | |
<cfset var jMsg = JavaCast("string",arguments.signMessage).getBytes("iso-8859-1") /> | |
<cfset var jKey = JavaCast("string",arguments.signKey).getBytes("iso-8859-1") /> | |
<cfset var key = createObject("java","javax.crypto.spec.SecretKeySpec") /> | |
<cfset var mac = createObject("java","javax.crypto.Mac") /> | |
<cfset key = key.init(jKey,"HmacSHA1") /> | |
<cfset mac = mac.getInstance(key.getAlgorithm()) /> | |
<cfset mac.init(key) /> | |
<cfset mac.update(jMsg) /> | |
<cfreturn mac.doFinal() /> | |
</cffunction> | |
<cffunction name="createSignature" returntype="string" access="public" output="false"> | |
<cfargument name="in" required="true" /> | |
<cfargument name="secretKey" required="true" /> | |
<!--- Replace "\n" with "chr(10) to get a correct digest ---> | |
<cfset var fixedData = replace(arguments.in,"\n", chr(10), "all") /> | |
<!--- Calculate the hash of the information ---> | |
<cfset var digest = HMAC_SHA1(arguments.secretKey,fixedData) /> | |
<!--- fix the returned data to be a proper signature ---> | |
<cfset var signature = ToBase64(digest) /> | |
<cfreturn signature /> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because S3 doesn't recognize directories, you will need to put the file name in the destination and remove the filefield.