Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Amazon S3 - Add Image (GIF) to BucketDemonstrates how to upload an image file (binary data) to an Amazon S3 bucket.
LOCAL loHttp LOCAL lnSuccess LOCAL loCrypt LOCAL lcCurDateTime LOCAL loGifData LOCAL loFac LOCAL lcObjName LOCAL lcMd5Hash LOCAL lcStrToSign LOCAL lcAWSAccessKeyId LOCAL lcAWSSecretAccessKey LOCAL lcSignature LOCAL lcAuthValue LOCAL lnBGzip LOCAL lnBMd5 LOCAL lcUrl LOCAL lcStrResponse loHttp = CreateObject('Chilkat.Http') lnSuccess = loHttp.UnlockComponent("Anything for 30-day trial.") IF (lnSuccess <> 1) THEN * Unlock failed. =MESSAGEBOX(loHttp.LastErrorText) QUIT ENDIF * We'll need this for HMAC and MD5... loCrypt = CreateObject('Chilkat.Crypt2') lnSuccess = loCrypt.UnlockComponent("Anything for 30-day trial.") IF (lnSuccess <> 1) THEN =MESSAGEBOX(loCrypt.LastErrorText) QUIT ENDIF * The HTTP component now includes a method to generate * the current date/time in RFC 2616 compliant format. * Note: The GenTimeStamp method is available as a pre-release (as of 18-June-2008). * It will become available in the next new version dated after * 18-June-2008. lcCurDateTime = loHttp.GenTimeStamp() loFac = CreateObject('Chilkat.FileAccess') loGifData = loFac.ReadEntireFile("dude.gif") * This is the name of the object to be added: lcObjName = "NeoGif" * Calculate the MD5 hash of the object's content: loCrypt.HashAlgorithm = "md5" loCrypt.EncodingMode = "base64" loCrypt.Charset = "windows-1252" lcMd5Hash = loCrypt.HashBytesENC(loGifData) * Create the string to be signed. * IMPORTANT: * If a Content-MD5 header is added (see below), then * you also need to include the MD5 hash of the content * here. * The content-type (image/gif) must match the content-type * passed to the PutBinary method (below). lcStrToSign = "PUT" + [CHR(10) + lcMd5Hash + [CHR(10) + "image/gif" + [CHR(10) + lcCurDateTime + [CHR(10) + "/chilkat/" + lcObjName * We want SHA1 for the HMAC hash algorithm: loCrypt.HashAlgorithm = "sha1" * These must be changed for your account: lcAWSAccessKeyId = "zzzzzzzzzzzzzzzzzzzzzz" lcAWSSecretAccessKey = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" * Set the HMAC secret key: loCrypt.SetHmacKeyString(lcAWSSecretAccessKey) * By setting the charset = "utf-8", the string will be converted * to utf-8 (internal to the Chilkat component) prior to signing: loCrypt.Charset = "utf-8" * Indicate that Base64 output is desired: loCrypt.EncodingMode = "base64" lcSignature = loCrypt.HmacStringENC(lcStrToSign) lcAuthValue = "AWS " + lcAWSAccessKeyId + ":" + lcSignature * The bucket to be used is specified in the Host header. * In this example, the object is added to the "chilkat" bucket: loHttp.SetRequestHeader("Host","chilkat.s3.amazonaws.com") loHttp.SetRequestHeader("Authorization",lcAuthValue) loHttp.SetRequestHeader("Date",lcCurDateTime) * Do not GZIP the request body. To send a gzip compressed * object, simply set this to 1 lnBGzip = 0 * Automatically add an MD5 hash of the request body in the HTTP header * (using the Content-MD5 header field). lnBMd5 = 1 lcUrl = "http://s3.amazonaws.com/" + lcObjName lcStrResponse = loHttp.PutBinary(lcUrl,loGifData,"image/gif",lnBMd5,lnBGzip) IF (loHttp.LastStatus = 200) THEN * Success is indicated by a response status of 200. =MESSAGEBOX("Object added to bucket!") * Let's check out the response header anyway... ? loHttp.LastResponseHeader ELSE * Failed. Show the last request header, response header, * and response body. ? loHttp.LastHeader ? "---" ? loHttp.LastResponseHeader ? "---" ? loHttp.LastErrorText ENDIF |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2007 Chilkat Software, Inc. All Rights Reserved.
Mail Component · .NET Email Component · ASP Mail Component · XML Parser