Following code helps you to upload file direct to AWS s3 from user browser using post request. this code is very efficient to process large files on small servers.
<cfparam name="S3Encryption" default="1">
<cfset bucketName = "test-bucket"/>
<cfset fileKey = "test.png"/>
<cfset contentType ='images/png'/>
<cfset HTTPtimeout = "300">
<cfset cacheControl true>
<cfset cacheDays = "30">
<cfset acl = "public-read">
<cfset storageClass = "STANDARD">
<cfset keyName = fileKey/>
<cfset versionID = "">
<cfscript>
if (application.s3ETAllowEncoding)
{
accessKeyId = application.S3ETKey;
Variables.secretAccessKey = application.S3ETPass;
}
else
{
Variables.accessKeyId = application.S3Key;
Variables.secretAccessKey = application.S3Pass;
}
Expiration = Dateformat(DateAdd("d", 7, "#now()#"), "yyyy-mm-dd")&'T00:00:00Z';
BucketName = bucketName; // Put your bucket name here
RedirectURL ='';
acl = acl;
ContenType = '#contentType#';
FileSize = 5000000000; // max allowed upload size in Bytes
Path = '';
</cfscript>
<cfsavecontent variable="S3policy">
<cfoutput>
{
"expiration": "#Expiration#",
"conditions": [
{"bucket": "#BucketName#"},
["starts-with", "$key", "#fileKey#"],
{"acl": "#acl#"},
["starts-with", "$Content-Type", "#ContenType#"],
{"success_action_redirect": "#RedirectURL#"},
["content-length-range", 0, #FileSize#]
,{"x-amz-server-side-encryption":"AES256"}
]
}
</cfoutput>
</cfsavecontent>
<cfset PolicyBase64 = ToBase64(S3policy)>
<cfhttp url="http://s3.amazonaws.com/test-bucket" method="post" multipart="yes">
<cfhttpparam type="formfield" name="key" value="#fileKey#" />
<cfhttpparam type="formfield" name="acl" value="#acl#" />
<cfhttpparam type="formfield" name="AWSAccessKeyId" value="#accessKeyId#" />
<cfhttpparam type="formfield" name="Policy" value="#PolicyBase64#" />
<cfhttpparam type="formfield" name="Signature" value="#createSignature(PolicyBase64)#" />
<cfhttpparam type="formfield" name="Content-Type" value="#ContenType#" />
<cfhttpparam type="formfield" name="success_action_redirect" value="" />
<cfhttpparam type="formfield" name="x-amz-server-side-encryption" value="AES256" />
<cfhttpparam type="file" name="file" file="#form.restfile#"/>
</cfhttp>
Get Object From S3 API using S3 API
How TO download file from S3 using S3
<cfset var epochTime = DateDiff("s", DateConvert("utc2Local", "January 1 1970 00:00"), now()) + (5)>
<cfset var signatureString = "GET\n\n\n#epochTime#\n/test-bucket/test.png">
!--- Create a proper signature --->
<cfset signature = createSignature(signatureString)>
!--- AWS Link for download file from s3 bucket--->
<cfset link = "http://s3.amazon.com/test-bucket/test.png?response-content-disposition=attachment;filename="test.png"&AWSAccessKeyId=#URLEncodedFormat(accessKeyId)#&Expires=#epochTime#&Signature=#URLEncodedFormat(signature)#">
!--- AWS normal Link --->
<cfset link = "http://s3.amazon.com/test-bucket/test.png?AWSAccessKeyId=#URLEncodedFormat(accessKeyId)#&Expires=#epochTime#&Signature=#URLEncodedFormat(signature)#">
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<!--- Create a canonical string to send based on operation requested --->
<cfset var cs = "DELETE\n\n\n#dateTimeString#\n/test-bucket/test.png">
<!--- Create a proper signature --->
<cfset var signature = createSignature(cs)>
<!--- delete the object via REST --->
<cfhttp method="DELETE" url="http://s3.amazon.com/test-bucket/test.png">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="Authorization" value="AWS #accessKeyId#:#signature#">
</cfhttp>
<cfset contentType = 'image/png'/>
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<Cfset acl = 'public-read'/>
<Cfset storageClass = 'STANDARD'/>
<!--- Create a canonical string to send --->
<cfset cs = "PUT\n\n#arguments.contentType#\n#dateTimeString#\nx-amz-acl:#arguments.acl#\nx-amz-storage class:#arguments.storageClass#\n/#arguments.bucketName#/#arguments.keyName#">
<!--- Create a proper signature --->
<cfset signature = createSignature(cs)>
<!--- Read the image data into a variable --->
<cffile action="readBinary" file="d:\\test.png" variable="binaryFileData">
<!--- Send the file to amazon. The "X-amz-acl" controls the access properties of the file --->
<cfhttp method="PUT" url="https://test-bucket.s3.amazonaws.com/test-bucket/test.png">
<cfhttpparam type="header" name="Authorization" value="AWS #accessKeyId#:#signature#">
<cfhttpparam type="header" name="Content-Type" value="#contentType#">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="x-amz-acl" value="#acl#">
<cfhttpparam type="header" name="x-amz-storage-class" value="#storageClass#">
<cfhttpparam type="body" value="#binaryFileData#">
<!--- cache control in AWS s3 object --->
<cfif cacheControl EQ true>
<cfhttpparam type="header" name="Cache-Control" value="max-age=2592000">
<cfhttpparam type="header" name="Expires" value="#DateFormat(now()+3,'ddd, dd mmm yyyy')# #TimeFormat(now(),'H:MM:SS')# GMT">
</cfif>
</cfhttp>