[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Amazon AWS woes ...

Posted on 2014-08-27
12
Medium Priority
?
274 Views
Last Modified: 2014-11-12
Please post the simplest of examples of using ColdFusion to post files to the S3.amazonaws.com server using an HTML5 PLUpload interface like the tutorial posted below.

Here is a link to a demo / tutorial that I referred to:
https://github.com/moxiecode/plupload/wiki/Upload-to-Amazon-S3

BTW - I get 403 forbidden ...
0
Comment
Question by:dgrafx
  • 6
  • 4
  • 2
12 Comments
 
LVL 14

Expert Comment

by:RickEpnet
ID: 40289288
What version of coldfusion are you using?
0
 
LVL 25

Author Comment

by:dgrafx
ID: 40289294
sorry forgot to add that - 8
0
 
LVL 14

Expert Comment

by:RickEpnet
ID: 40290321
That makes it a bit harder. They started including some AWS things in 10 I would not know how to do that in 8 maybe someone else does. Click request attention.
0
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
LVL 52

Expert Comment

by:_agx_
ID: 40290734
I can't test it, but the only parts that look php specific are generating the "policy" and "signature" values. It's just some hashing and base64 encoding so you should be able to do the same in CF8. Once you have got the values, I think it's just a matter of plugging in 4 CF variables instead of php variables here:

  
    url : 'http://<?php echo $bucket; ?>.s3.amazonaws.com/',
    ..
    multipart_params: {
       ...
        'AWSAccessKeyId' : '<?php echo $accessKeyId; ?>',       
        'policy': '<?php echo $policy; ?>',
        'signature': '<?php echo $signature; ?>'
    }

Open in new window



Edit:  There's a CF example of doing the same thing (just with a plain form post) here.


One question, does CF8 support nesting of implicit arrays and structures? I can't remember. Run this code, does it work or error out?

<cfscript>
	aaaa = {
		"bbbb"   = "test..."
		, "cccc" = [ {"ddd" = "123"}, { "eee" = "abcd"}]
	};
</cfscript>
						
<cfdump var="#aaaa#">

Open in new window

0
 
LVL 25

Author Comment

by:dgrafx
ID: 40290752
that code errors - i had to rewrite some cfscript algorithm code into tag based code for the encryption for that same reason.

I will post back - I had a new idea I wanted to try this morning but ran into network issues ...
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 40292843
Don't know if you figured it out, but I think I converted this example to CF8 syntax.  Only 2 things are unsupported:  hmac (cf10+) and nested {} and [].

For HMAC() you'll need a custom function like this one.  (Didn't have time to convert it all to cfscript. ) The rest is just breaking out the structures an arrays into single lines:

HMACEncrypt
<cffunction name="hmacEncrypt" returntype="binary" access="public" output="false">
   <cfargument name="signKey" type="string" required="true" />
   <cfargument name="signMessage" type="string" required="true" />
   <cfargument name="algorithm" type="string" default="HmacSHA1" />
   <cfargument name="charset" type="string" default="UTF-8" />

   <cfset var msgBytes = charsetDecode(arguments.signMessage, arguments.charset) />
   <cfset var keyBytes = charsetDecode(arguments.signKey, arguments.charset) />
   <cfset var keySpec = createObject("java","javax.crypto.spec.SecretKeySpec")  />
   <cfset var mac = createObject("java","javax.crypto.Mac") />

   <cfset key = keySpec.init(keyBytes, arguments.algorithm) />
   <cfset mac = mac.getInstance(arguments.algorithm) />
   <cfset mac.init(key) />
   <cfset mac.update(msgBytes) />

   <cfreturn mac.doFinal() />
</cffunction>

Open in new window



Generate policy and signature:

<cfscript>
	// Hard coded for demo purposes only 
	aws.bucket = "your bucket here";
 	aws.secretKey = "secret key here";
 	aws.accessId = "your id here";
	successUrl = "http://" & cgi.server_name & getDirectoryFromPath( cgi.script_name ) & "success.cfm";
 

	// CF8 does not support nesting of {} and [], so build array the long way
	conditions = [];	

	// append structures
	data = {"bucket" = aws.bucket};
	arrayAppend(conditions, data);
	data = { "acl" = "private"};
	arrayAppend(conditions, data);
	data = {"success_action_redirect" = successUrl};
	arrayAppend(conditions, data);
	
	// append arrays
	data = [ "starts-with", "$key", "/form-post/" ];
	arrayAppend(conditions, data);
	data = [ "starts-with", "$Content-Type", "image/" ];
	arrayAppend(conditions, data);
	data = [ "content-length-range", 0, 10485760 ];
	arrayAppend(conditions, data);
	
	policy = {
		"expiration"   = dateFormat( expiration, "yyyy-mm-dd" ) & "T" & timeFormat( expiration, "HH:mm:ss" ) & "Z" 
		, "conditions" = conditions
	};
	 

	serializedPolicy = serializeJson( policy );
	serializedPolicy = reReplace( serializedPolicy, "[\r\n]+", "", "all" );
	encodedPolicy = binaryEncode(charsetDecode( serializedPolicy, "utf-8" ) ,"base64");
	// Custom function for CF8
	// http://stackoverflow.com/a/2960258
	hashedPolicy = hmacEncrypt(aws.secretKey,encodedPolicy,"HmacSHA1","utf-8");
	encodedSignature = binaryEncode(hashedPolicy, "base64");
	
	//WriteDump(policy);
	//writeDump(encodedSignature);
</cfscript>

Open in new window


After that you should be able to just plug the CF variables into the config section

<cfoutput>
...
url : 'http://#aws.bucket#.s3.amazonaws.com/',
    ..
    multipart_params: {
       ...
        'AWSAccessKeyId' : '#aws.accessId#',       
        'policy': '#encodedPolicy#',
        'signature': '#encodedSignature#'
    }	
...	
</cfoutput>

Open in new window

0
 
LVL 25

Author Comment

by:dgrafx
ID: 40293243
will get back to you - really swamped with "stuff" ...
I do appreciate it!
0
 
LVL 25

Author Comment

by:dgrafx
ID: 40307620
sorry for the delay - it took me a while to get back to this ...

as it turns out the issue with my code was a secondary process which encodes the uploaded video.
i was shocked to discover that the file was arriving to amazon and that the error was occurring after that.
i originally thought that i had a connection error to amazon - because of the error message.

thanks and the code posted is pretty much the same as what I'm using for those of you reading this post.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40307855
Ugh.. the joys of troubleshooting processes involving remote servers. Glad you finally figured it out!
0
 
LVL 25

Author Comment

by:dgrafx
ID: 40307903
Yes - joys - I like that ...
0
 
LVL 25

Author Comment

by:dgrafx
ID: 40331803
_agx_ - here are some more points for ya (potentially anyway) ...

http://www.experts-exchange.com/Programming/Languages/Scripting/Cold_Fusion_Markup_Language/Q_28521522.html

Thanks
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40332502
Hey.  Won't be able to look it over till tomorrow. But have you looked at the zencoder project on riaforge? I only skimmed the example, but .. looks like it works with S3.

https://github.com/sctm/zencoder-cf/tree/master/examples
0

Featured Post

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article provides a guide on how to optimise your costs within your AWS infrastructure when using some of the common services such as EC2, EBS, S3, Glacier, CloudFront, EIP & ELB.
Microservice architecture adoption brings many advantages, but can add intricacy. Selecting the right orchestration tool is most important for business specific needs.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question