Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
|
Amazon S3 - Add Text Object to BucketThis example writes some text and metadata into the "Neo" object in the "chilkat" bucket:
CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @sTmp0 nvarchar(4000) DECLARE @http int EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @http, 'UnlockComponent', @success OUT, 'Anything for 30-day trial.' IF @success <> 1 BEGIN -- Unlock failed. EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END -- We'll need this for HMAC and MD5... DECLARE @crypt int EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END EXEC sp_OAMethod @crypt, 'UnlockComponent', @success OUT, 'Anything for 30-day trial.' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END -- 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. DECLARE @curDateTime nvarchar(4000) EXEC sp_OAMethod @http, 'GenTimeStamp', @curDateTime OUT -- This is the content of the text object to be added to -- the "chilkat" bucket: DECLARE @textData nvarchar(4000) SELECT @textData = 'woah' -- This is the name of the object to be added: DECLARE @objName nvarchar(4000) SELECT @objName = 'Neo' -- Calculate the MD5 hash of the object's content: EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'md5' EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64' EXEC sp_OASetProperty @crypt, 'Charset', 'windows-1252' DECLARE @md5Hash nvarchar(4000) EXEC sp_OAMethod @crypt, 'HashStringENC', @md5Hash OUT, @textData -- 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 (text/plain) must match the content-type -- passed to the PutText method (below). DECLARE @strToSign nvarchar(4000) ERROR-CONCAT SELECT @strToSign = 'PUT' + CHAR(10) + @md5Hash + CHAR(10) + 'text/plain' + CHAR(10) + @curDateTime + CHAR(10) + '/chilkat/' + @objName -- We want SHA1 for the HMAC hash algorithm: EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'sha1' -- These must be changed for your account: DECLARE @AWSAccessKeyId nvarchar(4000) SELECT @AWSAccessKeyId = 'zzzzzzzzzzzzzzzzzzzz' DECLARE @AWSSecretAccessKey nvarchar(4000) SELECT @AWSSecretAccessKey = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzz' -- Set the HMAC secret key: EXEC sp_OAMethod @crypt, 'SetHmacKeyString', NULL, @AWSSecretAccessKey -- By setting the charset = "utf-8", the string will be converted -- to utf-8 (internal to the Chilkat component) prior to signing: EXEC sp_OASetProperty @crypt, 'Charset', 'utf-8' -- Indicate that Base64 output is desired: EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64' DECLARE @signature nvarchar(4000) EXEC sp_OAMethod @crypt, 'HmacStringENC', @signature OUT, @strToSign DECLARE @authValue nvarchar(4000) ERROR-CONCAT SELECT @authValue = 'AWS ' + @AWSAccessKeyId + ':' + @signature -- The bucket to be used is specified in the Host header. -- In this example, the object is added to the "chilkat" bucket: EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Host', 'chilkat.s3.amazonaws.com' EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Authorization', @authValue EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Date', @curDateTime -- Do not GZIP the request body. To send a gzip compressed -- object, simply set this to 1 DECLARE @bGzip int SELECT @bGzip = 0 -- Automatically add an MD5 hash of the request body in the HTTP header -- (using the Content-MD5 header field). DECLARE @bMd5 int SELECT @bMd5 = 1 DECLARE @url nvarchar(4000) ERROR-CONCAT SELECT @url = 'http://s3.amazonaws.com/' + @objName DECLARE @strResponse nvarchar(4000) EXEC sp_OAMethod @http, 'PutText', @strResponse OUT, @url, @textData, 'windows-1252', 'text/plain', @bMd5, @bGzip EXEC sp_OAGetProperty @http, 'LastStatus', @sTmp0 OUT IF @sTmp0 = 200 BEGIN PRINT 'Object added to bucket!' -- Let's check out the response header anyway... EXEC sp_OAGetProperty @http, 'LastResponseHeader', @sTmp0 OUT PRINT @sTmp0 END ELSE BEGIN -- Failed. Show the last request header, response header, -- and response body. EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT PRINT @sTmp0 PRINT '---' EXEC sp_OAGetProperty @http, 'LastResponseHeader', @sTmp0 OUT PRINT @sTmp0 PRINT '---' EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 END END GO |
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.