The following code sample illustrates how to use the SOAP interface to S3:
import sha, hmac, base64, urllib # list buckets for Amazon s3 AWSAccessKeyId='[AWSAccessKeyID]' AWSSecretAccessKey = '[AWSSecretAccessKey]' from SOAPpy import WSDL import sha def calcSig(key,text): import hmac, base64 sig = base64.b64encode(hmac.new(key, text, sha).digest()) return sig def ListMyBuckets(s): from time import gmtime,strftime method = 'ListAllMyBuckets' ts = strftime("%Y-%m-%dT%H:%M:%S.000Z", gmtime()) text = 'AmazonS3' + method + ts sig = calcSig(AWSSecretAccessKey,text) print "ListMyBuckets: ts,text,sig->", ts, text, sig return s.ListAllMyBuckets(AWSAccessKeyId=AWSAccessKeyId, Â Timestamp=ts,Signature=sig) def CreateBucket(s, bucketName): from time import gmtime,strftime method = 'CreateBucket' print 'method: ', method ts = strftime("%Y-%m-%dT%H:%M:%S.000Z", gmtime()) text = 'AmazonS3' + method + ts sig = calcSig(AWSSecretAccessKey,text) print "CreateBuckets: ts,text,sig->", ts, text, sig return s.CreateBucket(Bucket=bucketName, AWSAccessKeyId=AWSAccessKeyId, Â Timestamp=ts,Signature=sig) if __name__ == '__main__': s = WSDL.Proxy("http://s3.amazonaws.com/doc/2006-03-01/AmazonS3.wsdl") print ListMyBuckets(s) CreateBucket(s,"test20071126RY") print ListMyBuckets(s)
You can learn the following about S3 from this code:
As with the REST interface, you need to have an Amazon.com AWS access key ID and secret access key, which you can get if you sign up for an account at AWS (see Chapter 7 or the “Signing Up for Amazon AWS” sidebar earlier in this chapter for more details).
Although S3 is accessible by the REST and SOAP interfaces, this code uses SOAP and WSDL. The WSDL for the service at the time of writing is at http://s3.amazonaws.com/?doc/2006-03-01/AmazonS3.wsdl
; you can get the location of the latest WSDL URL at http://aws.amazon.com/s3
.
Two methods are used in this code sample: ListMyBuckets
and CreateBuckets
. You can get the list of all the methods at http://www.awszone.com/scratchpads/aws/?s3.us/?index.?aws
(the technical documentation is at http://developer.amazonwebservices.com/
connect/?
kbcategory.jspa?categoryID=48
, which leads to http://docs.amazonwebservices
.?com/?Ama
zonS3/2006-03-01/
).
Note that one of the complicated aspects is to calculate a signature, something you learned to do in the previous section.