Okay I warned you, it’s ugly, but here are the a few of the functions I built to get simpleDB working:
First, I created two variables, aKey and sKey, to hold my Amazon access and secret keys, respectively.
Be sure to include the following required modules:
local crypto = require("crypto")
local mime = require("mime")
The first set of code is my quick solution to creating a timestamp:
function twoDigit(n)
local r = tostring(n)
if string.len(r) \< 2 then
r = "0"..r;
end
return r
end
function getTime()
local date = os.date("!\*t")
return date.year.."-"..twoDigit(date.month).."-"..twoDigit(date.day).."T"..twoDigit(date.hour).."%3A"..twoDigit(date.min).."%3A"..twoDigit(date.sec).."Z"
end
The next piece of code is the hash generator signature thingy (yes, technical term) borrowed from bob.dickinson (thanks Bob!)
local function sha1\_hmac( key, text )
return crypto.hmac(crypto.sha1, text, key, true)
end
The following is my Create a Domain function, where the passed-in-variable “name” is the name to call the domain.
function createDomain(name)
local timeStamp = getTime()
local toSign = "GET\nsdb.amazonaws.com\n/\nAWSAccessKeyId="..aKey.."&Action=CreateDomain&DomainName="..name.."&SignatureMethod=HmacSHA1&SignatureVersion=2&Timestamp="..timeStamp.."&Version=2009-04-15"
local signature = mime.b64(sha1\_hmac(sKey, toSign))
local myURL = "https://sdb.amazonaws.com/?Action=CreateDomain&AWSAccessKeyId="..aKey.."&DomainName="..name.."&SignatureVersion=2&SignatureMethod=HmacSHA1&Timestamp="..timeStamp.."&Version=2009-04-15&Signature="..signature
local function networkListener( event )
if ( event.isError ) then
print( "Network error!")
else
print ( "RESPONSE: " .. event.response )
save(event.response)
end
end
network.request( myURL, "GET", networkListener )
end
This next code lists the domains:
function listDomains()
local timeStamp = getTime()
local toSign = "GET\nsdb.amazonaws.com\n/\nAWSAccessKeyId="..aKey.."&Action=ListDomains&SignatureMethod=HmacSHA1&SignatureVersion=2&Timestamp="..timeStamp.."&Version=2009-04-15"
local signature = mime.b64(sha1\_hmac(sKey, toSign))
local myURL = "https://sdb.amazonaws.com/?Action=ListDomains&AWSAccessKeyId="..aKey.."&SignatureVersion=2&SignatureMethod=HmacSHA1&Timestamp="..timeStamp.."&Version=2009-04-15&Signature="..signature
local function networkListener( event )
if ( event.isError ) then
print( "Network error!")
else
print ( "RESPONSE: " .. event.response )
save(event.response)
end
end
network.request( myURL, "GET", networkListener )
end
This next code performs a “Get Attributes” call. Please note that I hard-coded into the strings the attribute and item names just to get the code tested. Like I said, I shelved this project due to other concerns. Again, the passed-in-variable “name” is the domain you are checking.
function getAttributes(name)
local timeStamp = getTime()
local toSign = "GET\nsdb.amazonaws.com\n/\nAWSAccessKeyId="..aKey.."&Action=GetAttributes&AttributeName.1=theTest&DomainName="..name.."&ItemName=item\_01&SignatureMethod=HmacSHA1&SignatureVersion=2&Timestamp="..timeStamp.."&Version=2009-04-15"
local signature = mime.b64(sha1\_hmac(sKey, toSign))
local myURL = "https://sdb.amazonaws.com/?Action=GetAttributes&AWSAccessKeyId="..aKey.."&AttributeName.1=theTest&DomainName="..name.."&ItemName=item\_01&SignatureVersion=2&SignatureMethod=HmacSHA1&Timestamp="..timeStamp.."&Version=2009-04-15&Signature="..signature
local function networkListener( event )
if ( event.isError ) then
print( "Network error!")
else
print ( "RESPONSE: " .. event.response )
save(event.response)
end
end
network.request( myURL, "GET", networkListener )
end
So, there you go. Frankencode. But it should get you started. If someone makes use of this please consider submitting a cleaned up version to the code base. Thanks. [import]uid: 64596 topic_id: 22449 reply_id: 96205[/import]