Sample code for 30+ languages & platforms
SQL Server

Download S3 CloudTrail Log and Un-Gzip

See more Amazon S3 Examples

Demonstrates how to download a Amazon CloudTrail log from an S3 bucket. The file in this example is a .json.gz. The file is uncompressed and the JSON parsed.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  This requires the Chilkat API to have been previously unlocked.
    --  See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @http, 'AwsAccessKey', 'AWS_ACCESS_KEY'
    EXEC sp_OASetProperty @http, 'AwsSecretKey', 'AWS_SECRET_KEY'
    EXEC sp_OASetProperty @http, 'AwsRegion', 'us-west-2'
    EXEC sp_OASetProperty @http, 'AwsEndpoint', 's3-us-west-2.amazonaws.com'

    DECLARE @bucketName nvarchar(4000)
    SELECT @bucketName = 'chilkat.logs'
    DECLARE @objectName nvarchar(4000)
    SELECT @objectName = '/AWSLogs/957491831129/CloudTrail/us-west-1/2016/11/12/957491831129_CloudTrail_us-west-1_20161112T1335Z_umXfD4RxHE5nDGuI.json.gz'
    DECLARE @localFilePath nvarchar(4000)
    SELECT @localFilePath = 'qa_output/cloudTrailLog.json.gz'

    EXEC sp_OAMethod @http, 'S3_DownloadFile', @success OUT, @bucketName, @objectName, @localFilePath
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        RETURN
      END

    DECLARE @statusCode int
    EXEC sp_OAGetProperty @http, 'LastStatus', @statusCode OUT
    IF @statusCode <> 200
      BEGIN

        PRINT 'Failed to download, response status code = ' + @statusCode
        EXEC @hr = sp_OADestroy @http
        RETURN
      END

    DECLARE @jsonPath nvarchar(4000)
    SELECT @jsonPath = 'qa_output/cloudTrailLog.json'
    DECLARE @gzip int
    EXEC @hr = sp_OACreate 'Chilkat.Gzip', @gzip OUT

    EXEC sp_OAMethod @gzip, 'UncompressFile', @success OUT, @localFilePath, @jsonPath
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @gzip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @gzip
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'LoadFile', @success OUT, @jsonPath
    EXEC sp_OASetProperty @json, 'EmitCompact', 0
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    --  Sample JSON is shown below.
    --  Go to http://tools.chilkat.io/jsonParse.cshtml 
    --  and copy/paste the JSON into the online tool to generate parsing code.

    --  {
    --    "Records": [
    --      {
    --        "eventVersion": "1.05",
    --        "userIdentity": {
    --          "type": "Root",
    --          "principalId": "954591834127",
    --          "arn": "arn:aws:iam::954591834127:root",
    --          "accountId": "954591834127",
    --          "accessKeyId": "ASIAJ3DEMXUXI43B6FYQ",
    --          "sessionContext": {
    --            "attributes": {
    --              "mfaAuthenticated": "false",
    --              "creationDate": "2016-11-12T13:09:31Z"
    --            }
    --          }
    --        },
    --        "eventTime": "2016-11-12T13:34:39Z",
    --        "eventSource": "cloudtrail.amazonaws.com",
    --        "eventName": "DescribeTrails",
    --        "awsRegion": "us-west-1",
    --        "sourceIPAddress": "98.228.98.57",
    --        "userAgent": "console.amazonaws.com",
    --        "requestParameters": {
    --          "trailNameList": [
    --          ]
    --        },
    --        "responseElements": null,
    --        "requestID": "c2a0376c-a8dc-11e6-89e7-85ac5ce5ee56",
    --        "eventID": "eb5afd70-dae1-41f0-82b4-91c3c936d9ba",
    --        "eventType": "AwsApiCall",
    --        "recipientAccountId": "954591834127"
    --      }
    --    ]
    --  }

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @gzip
    EXEC @hr = sp_OADestroy @json


END
GO