Sample code for 30+ languages & platforms
SQL Server

Backblaze S3 List Bucket Objects

See more Backblaze S3 Examples

Demonstrates how to list the objects in a bucket.

The Chilkat S3 functions in the HTTP class are compatible with the Backblaze service. However, because of some specific issues, Chilkat v9.5.0.89 or later is needed.

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 @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes the Chilkat HTTP 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

    -- keyID = Access Key ID or Access Key
    EXEC sp_OASetProperty @http, 'AwsAccessKey', 'access-key'

    -- applicationKey = Secret Access Key or Secret Key
    EXEC sp_OASetProperty @http, 'AwsSecretKey', 'secret-key'

    -- Region is the 2nd part of your S3 Endpoint
    EXEC sp_OASetProperty @http, 'AwsEndpoint', 's3.us-west-002.backblazeb2.com'

    DECLARE @bucketName nvarchar(4000)
    SELECT @bucketName = 'chilkat-test'

    DECLARE @strXml nvarchar(4000)
    EXEC sp_OAMethod @http, 'S3_ListBucketObjects', @strXml OUT, @bucketName
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        RETURN
      END


    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    PRINT 'Response status code = ' + @iTmp0

    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT

    EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @strXml
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @xml, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @xml
        RETURN
      END

    -- If the response status code was not 200, then the XML response is not a 
    -- listing of objects, but instead contains error information.
    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN
        EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @xml
        RETURN
      END

    -- A sample response is shown below.
    EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

    PRINT '----'

    -- Use this online tool to generate parsing code from sample XML: 
    -- Generate Parsing Code from XML

    -- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    -- <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    --     <Contents>
    --         <ETag>"303fb46cf341094fef6274b7789cd6aa"</ETag>
    --         <Key>orchard.json</Key>
    --         <LastModified>2021-10-30T14:45:52.000Z</LastModified>
    --         <Owner>
    --             <ID>f2ebbc5f792c</ID>
    --             <DisplayName/>
    --         </Owner>
    --         <Size>22</Size>
    --         <StorageClass>STANDARD</StorageClass>
    --     </Contents>
    --     <Contents>
    --         <ETag>"a8551f0a5437f43a796fca7623ee9232"</ETag>
    --         <Key>seahorse.jpg</Key>
    --         <LastModified>2021-10-30T14:38:53.000Z</LastModified>
    --         <Owner>
    --             <ID>f2ebbc5f792c</ID>
    --             <DisplayName/>
    --         </Owner>
    --         <Size>24388</Size>
    --         <StorageClass>STANDARD</StorageClass>
    --     </Contents>
    --     <IsTruncated>false</IsTruncated>
    --     <MaxKeys>1000</MaxKeys>
    --     <Name>chilkat-test</Name>
    --     <Prefix/>
    --     <Marker/>
    -- </ListBucketResult>

    DECLARE @ETag nvarchar(4000)

    DECLARE @Key nvarchar(4000)

    DECLARE @LastModified nvarchar(4000)

    DECLARE @ID nvarchar(4000)

    DECLARE @SizeDecimalStr nvarchar(4000)

    DECLARE @StorageClass nvarchar(4000)

    DECLARE @ListBucketResult_xmlns nvarchar(4000)
    EXEC sp_OAMethod @xml, 'GetAttrValue', @ListBucketResult_xmlns OUT, 'xmlns'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @xml, 'NumChildrenHavingTag', @count_i OUT, 'Contents'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @xml, 'I', @i
        EXEC sp_OAMethod @xml, 'GetChildContent', @ETag OUT, 'Contents[i]|ETag'
        EXEC sp_OAMethod @xml, 'GetChildContent', @Key OUT, 'Contents[i]|Key'
        EXEC sp_OAMethod @xml, 'GetChildContent', @LastModified OUT, 'Contents[i]|LastModified'
        EXEC sp_OAMethod @xml, 'GetChildContent', @ID OUT, 'Contents[i]|Owner|ID'
        EXEC sp_OAMethod @xml, 'GetChildContent', @SizeDecimalStr OUT, 'Contents[i]|Size'
        EXEC sp_OAMethod @xml, 'GetChildContent', @StorageClass OUT, 'Contents[i]|StorageClass'
        SELECT @i = @i + 1
      END
    DECLARE @IsTruncated nvarchar(4000)
    EXEC sp_OAMethod @xml, 'GetChildContent', @IsTruncated OUT, 'IsTruncated'
    DECLARE @MaxKeys int
    EXEC sp_OAMethod @xml, 'GetChildIntValue', @MaxKeys OUT, 'MaxKeys'
    DECLARE @Name nvarchar(4000)
    EXEC sp_OAMethod @xml, 'GetChildContent', @Name OUT, 'Name'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @xml


END
GO