LotusScript AmazonS3Helper Class

I’ve refactored my AmazonS3 example by creating a AmazonS3Helper class. The AmazonS3Helper class builds the datestamp and signature you need for every call.

I have taken this code:

Function FormatDateToISO8601(aDate As NotesDateTime) As String
	' Thanks to Joel Litton http://www.joelitton.net/A559B2/home.nsf/d6plinks/JLIN-5UU4B2
	Dim dtLocal As NotesDateTime
	Dim dtGMT As NotesDateTime
	Set dtGMT = New NotesDateTime( Left$(aDate.GMTTime, 22) )
	FormatDateToISO8601= Format$(dtGMT.LSLocalTime, "yyyy-mm-ddThh:nn:ss.000Z")
End Function

Sub Initialize

	Const mysecretkey = PUT YOUR SECRET KEY HERE
	Const myAccessKey = PUT YOUR ACCESS KEY HERE
	Dim myS3 As New AmazonS3_n3()
	Dim mydate As New XSD_DATETIME
	Dim mykey As New XSD_STRING
	Dim mysig As New XSD_STRING

	Dim dateTime As New NotesDateTime( "" )
	Dim hmacbuilder As String
	Dim formattedDate As String
	Call dateTime.SetNow

	Dim mybuckets As ListAllMyBucketsResult_n3
	formattedDate = FormatDateToISO8601(dateTime)
	Call mydate.setValueFromString(formattedDate)
	Call mykey.setValueFromString(myaccesskey)
	HMACBuilder = "AmazonS3ListAllMyBuckets" + formattedDate
	Call mysig.setValueFromString(HMAC_SHA1_B64(mysecretkey,HMACBuilder))
	Set mybuckets =  myS3.ListAllMyBuckets(mykey,mydate,mysig)

End Sub

and turned it into this:

	Dim myS3 As New AmazonS3_n3()
	Dim S3Helper As AmazonS3Helper

	Dim mybuckets As ListAllMyBucketsResult_n3
	Set S3Helper = New AmazonS3Helper("ListAllMyBuckets")
	Set mybuckets =  myS3.ListAllMyBuckets(S3Helper.key,S3Helper.DateStamp,S3Helper.Signature)

The class takes one parameter in the constructor: the AmazonS3 function you wish to call. In the case above, I sent it “ListAllMyBuckets.” The class then generates the key, datestamp, and signature needed for the call. You can also make a “Regenerate” call to regenerate the values using the current date and time.

You could also set a new function name with the “S3Function” property. Setting the S3Function property also calls the “regenerate” function mentioned previously.

Example:

	Dim myS3 As New AmazonS3_n3()
	Dim S3Helper As AmazonS3Helper

	Dim mybuckets As ListAllMyBucketsResult_n3
	Set S3Helper = New AmazonS3Helper("ListAllMyBuckets")
	Set mybuckets =  myS3.ListAllMyBuckets(S3Helper.key,S3Helper.DateStamp,S3Helper.Signature)

	Dim mybucket As CreateBucketResult_n3
	Dim acl As New  AccessControlList_n3()
	S3Helper.S3Function = "CreateBucket"
	Set mybucket = myS3.CreateBucket("testLotus", acl,S3Helper.key,S3Helper.DateStamp,S3Helper.Signature)

The library requires the HMAC_SHA1 script library found on this site and the AmazonS3 WSDL’d script library.

Download the AmazonS3Helper script library here: AmazonS3Helper

14 January 2008 | lotus, parenting | Comments

Comments:

  1.  
  2.  
  3.