jriver12
asked on
Beating my head with query to Amazon AWS with coldfusion
I have been beating my head senseless for sometime now with trying to query amazon's aws. When I run my code I get an error stating that the signature is invalid so now I turn to you Experts for your valued help.
Here is my code.
<cffunction name="awsurl" returntype="string" access="public" output="No">
<cfargument name="Query" type="string" required="true" />
<cfargument name="AWSAccessKeyId" type="string" required="true" default="AKIAJxxxxxxxx"/>
<cfargument name="SecretKey" type="string" required="true" default="xxxxxxxxxxxxx>
<cfargument name="Host" type="string" default="ec2.amazonaws.com " />
<cfargument name="Methord" type="string" default="GET" />
<cfargument name="URI" type="string" default="/" />
<cfargument name="SignatureVersion" type="string" default="2" />
<cfargument name="Version" type="date" default="2012-04-01" />
<cfargument name="http" type="string" default="https" hint="https|http" />
<!--- add common values --->
<cfset local.Time = dateConvert("local2Utc",no w())>
<cfset local.Time = "#DateFormat(local.Time,'y yyy-mm-dd' )#T#TimeFo rmat(local .Time,'HH: mm:ss')#">
<cfset arguments.Query = "#arguments.Query#&Timesta mp=#local. Time#&Sign atureVersi on=#argume nts.Signat ureVersion #&Version= #arguments .Version#& SignatureM ethod=Hmac SHA256">
<!--- sort QueryString --->
<cfset arguments.Query = ListToArray(arguments.Quer y,'&')>
<cfset ArraySort(arguments.Query, 'text')>
<!--- prepend AccessKeyID --->
<cfset ArrayPrepend(arguments.Que ry, "AWSAccessKeyId=#arguments .AWSAccess KeyId#")>
<!--- url encode each values --->
<cfset local.SortedString = ArrayNew(1)>
<cfloop from="1" to="#ArrayLen(arguments.Qu ery)#" index="local.i">
<cfset local.value = arguments.Query[local.i]>
<cfif listlen(local.value,'=') gt 1>
<cfset ArrayAppend(local.SortedSt ring,"#lis tfirst(loc al.value,' =')#=#rese rvedEncod( listlast(l ocal.value ,'='))#")>
<cfelse>
<cfset ArrayAppend(local.SortedSt ring,"#loc al.value#" )>
</cfif>
</cfloop>
<!--- create signature string --->
<cfset local.toEncode = "#arguments.Methord##chr(1 0)##argume nts.Host## chr(10)##a rguments.U RI##chr(10 )##ArrayTo List(local .SortedStr ing,'&')#" >
<cfoutput><pre>#local.toEn code#</pre ></cfoutpu t>
<!--- encode Signature String --->
<cfset local.Signature = URLEncodedFormat(ToBase64( HMAC_SHA25 6(local.to Encode,arg uments.Sec retKey)))>
<cfset arguments.Query = ArrayToList(arguments.Quer y,'&')>
<cfset arguments.Query = "#arguments.http#://#argum ents.Host# #arguments .URI#?#arg uments.Que ry#&Signat ure=#local .Signature #">
<cfreturn arguments.Query>
</cffunction>
<!--- ************************** ********** ********** ********** ********** *** --->
<cffunction name="reservedEncod" returntype="string" access="public" output="no">
<cfargument name="string" type="string" required="true" />
<cfset local.reserved = "!|##|$|&|'|(|)|*|+|,|/|:| ;|=|?|@|[| ]| ">
<cfloop list="#local.reserved#" index="local.i" delimiters="|">
<cfif find(local.i,arguments.str ing)>
<cfset arguments.string = replace(arguments.string,l ocal.i, "%#ucase(FormatBaseN(Asc(l ocal.i),'1 6'))#", 'all')>
</cfif>
</cfloop>
<cfreturn arguments.string>
</cffunction>
<!--- ************************** ********** ********** ********** ********** *** --->
<cffunction name="HMAC_SHA256" returntype="binary" access="public" output="no">
<cfargument name="signMessage" type="string" required="true" />
<cfargument name="signKey" type="string" required="true" />
<cfset local.jMsg = JavaCast("string",argument s.signMess age).getBy tes("iso-8 859-1") />
<cfset local.jKey = JavaCast("string",argument s.signKey) .getBytes( "iso-8859- 1") />
<cfset local.key = createObject("java","javax .crypto.sp ec.SecretK eySpec") />
<cfset local.mac = createObject("java","javax .crypto.Ma c") />
<cfset local.key = local.key.init(local.jKey, "HmacSHA25 6") />
<cfset local.mac = local.mac.getInstance(loca l.key.getA lgorithm() ) />
<cfset local.mac.init(local.key) />
<cfset local.mac.update(local.jMs g) />
<cfreturn local.mac.doFinal() />
</cffunction>
Here is my code.
<cffunction name="awsurl" returntype="string" access="public" output="No">
<cfargument name="Query" type="string" required="true" />
<cfargument name="AWSAccessKeyId" type="string" required="true" default="AKIAJxxxxxxxx"/>
<cfargument name="SecretKey" type="string" required="true" default="xxxxxxxxxxxxx>
<cfargument name="Host" type="string" default="ec2.amazonaws.com
<cfargument name="Methord" type="string" default="GET" />
<cfargument name="URI" type="string" default="/" />
<cfargument name="SignatureVersion" type="string" default="2" />
<cfargument name="Version" type="date" default="2012-04-01" />
<cfargument name="http" type="string" default="https" hint="https|http" />
<!--- add common values --->
<cfset local.Time = dateConvert("local2Utc",no
<cfset local.Time = "#DateFormat(local.Time,'y
<cfset arguments.Query = "#arguments.Query#&Timesta
<!--- sort QueryString --->
<cfset arguments.Query = ListToArray(arguments.Quer
<cfset ArraySort(arguments.Query,
<!--- prepend AccessKeyID --->
<cfset ArrayPrepend(arguments.Que
<!--- url encode each values --->
<cfset local.SortedString = ArrayNew(1)>
<cfloop from="1" to="#ArrayLen(arguments.Qu
<cfset local.value = arguments.Query[local.i]>
<cfif listlen(local.value,'=') gt 1>
<cfset ArrayAppend(local.SortedSt
<cfelse>
<cfset ArrayAppend(local.SortedSt
</cfif>
</cfloop>
<!--- create signature string --->
<cfset local.toEncode = "#arguments.Methord##chr(1
<cfoutput><pre>#local.toEn
<!--- encode Signature String --->
<cfset local.Signature = URLEncodedFormat(ToBase64(
<cfset arguments.Query = ArrayToList(arguments.Quer
<cfset arguments.Query = "#arguments.http#://#argum
<cfreturn arguments.Query>
</cffunction>
<!--- **************************
<cffunction name="reservedEncod" returntype="string" access="public" output="no">
<cfargument name="string" type="string" required="true" />
<cfset local.reserved = "!|##|$|&|'|(|)|*|+|,|/|:|
<cfloop list="#local.reserved#" index="local.i" delimiters="|">
<cfif find(local.i,arguments.str
<cfset arguments.string = replace(arguments.string,l
</cfif>
</cfloop>
<cfreturn arguments.string>
</cffunction>
<!--- **************************
<cffunction name="HMAC_SHA256" returntype="binary" access="public" output="no">
<cfargument name="signMessage" type="string" required="true" />
<cfargument name="signKey" type="string" required="true" />
<cfset local.jMsg = JavaCast("string",argument
<cfset local.jKey = JavaCast("string",argument
<cfset local.key = createObject("java","javax
<cfset local.mac = createObject("java","javax
<cfset local.key = local.key.init(local.jKey,
<cfset local.mac = local.mac.getInstance(loca
<cfset local.mac.init(local.key) />
<cfset local.mac.update(local.jMs
<cfreturn local.mac.doFinal() />
</cffunction>
ASKER
my third attempt see attached. (error with complex variables)
amazonint3.txt
amazonint3.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
got it working using this method(attached) however it is only returning the first few of 100/1000 results. I need to view and return all and am hitting a wall. any suggestions would be great.
EEF2.txt
EEF2.txt
look at some of this documentation
http://docs.aws.amazon.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/get_object_list
http://docs.aws.amazon.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/get_object_list
ASKER
Thanks for the pointer but Im not using php, I'm Using ColdFusion.
Not to mention I am clueless on php.
Not to mention I am clueless on php.
yes - i know you are on CF.
you are typically hard pressed to find CF code examples - especially from large companies - so we usually read through PHP or whatever and glean what we can understand.
note that you don't need to understand everything - just looking for clues ...
you are typically hard pressed to find CF code examples - especially from large companies - so we usually read through PHP or whatever and glean what we can understand.
note that you don't need to understand everything - just looking for clues ...
I've requested that this question be deleted for the following reason:
The question has either no comments or not enough useful information to be called an "answer".
The question has either no comments or not enough useful information to be called an "answer".
I would say that I answered his question - see this post: http:#a40476395 but poster then asked for help with the number of records it returned - which was not in the original question.
I did try to provide more help but admittedly did not provide a lot of useful additional info.
Thanks ...
I did try to provide more help but admittedly did not provide a lot of useful additional info.
Thanks ...
ASKER
Points have been awarded for answer. Dgrafx is correct in declaring that an answer was provided which helped in resolving initial issue.
Thanks !
ASKER
Thank you for your assistance, excuse my delay.
ASKER
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Amazon</title>
</head>
<body>
<div class="container">
</div>
<script src="jquery.js"></script>
<script src="jquery.xdomainajax.js
<script>
$(document).ready(function
$.ajax({
url: 'http://ecs.amazonaws.com/onca/xml?'+
'AWSAccessKeyId=xxxxxxxxxx
'&AssociateTag=xxxxxxxx'+
'&Keywords=B004C5P522'+
'&Operation=ItemSearch'+
'&SearchIndex=Billards-Spo
'&Service=AWSECommerceServ
'&Timestamp=2014-07-17T17%
'&Signature=xxxxxxxxxxx,
type: 'get',
dataType: "xml",
success: function(res) {
console.log(res.responseTe
var xml = $.parseXML(res.responseTex
var $tmp = $( xml );
var tmp2 = $tmp.find( "head" );
$('.container').append(tmp
},
error: function(){
alert('failed')
}
});
});
</script>
</body>
</html>