Sample code for 30+ languages & platforms
SQL Server

OSS List Buckets (Alibaba Cloud)

See more Alibaba Cloud OSS Examples

Demonstrates how to list AliCloud OSS buckets.

The Chilkat S3 functions in the HTTP class are compatible with Alibaba Cloud's OSS service.

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 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

    -- Insert your AccessKey ID here:
    EXEC sp_OASetProperty @http, 'AwsAccessKey', 'access-key'

    -- Insert your AccessKey Secret here:
    EXEC sp_OASetProperty @http, 'AwsSecretKey', 'secret-key'

    -- See Alibaba Object Storage Service Regions and Endpoints
    EXEC sp_OASetProperty @http, 'AwsEndpoint', 'oss-us-west-1.aliyuncs.com'

    DECLARE @bucketsXml nvarchar(4000)
    EXEC sp_OAMethod @http, 'S3_ListBuckets', @bucketsXml OUT

    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

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

    EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @bucketsXml
    EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

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

    -- Sample output:

    -- <?xml version="1.0" encoding="UTF-8"?>
    -- <ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    --     <Owner>
    --         <ID>5035535379748121</ID>
    --         <DisplayName>5035535379748121</DisplayName>
    --     </Owner>
    --     <Buckets>
    --         <Bucket>
    --             <Comment/>
    --             <CreationDate>2021-10-28T21:30:56.000Z</CreationDate>
    --             <ExtranetEndpoint>oss-us-east-1.aliyuncs.com</ExtranetEndpoint>
    --             <IntranetEndpoint>oss-us-east-1-internal.aliyuncs.com</IntranetEndpoint>
    --             <Location>oss-us-east-1</Location>
    --             <Name>chilkat</Name>
    --             <Region>us-east-1</Region>
    --             <StorageClass>STANDARD</StorageClass>
    --         </Bucket>
    --         <Bucket>
    --             <Comment/>
    --             <CreationDate>2021-10-29T00:12:54.000Z</CreationDate>
    --             <ExtranetEndpoint>oss-us-west-1.aliyuncs.com</ExtranetEndpoint>
    --             <IntranetEndpoint>oss-us-west-1-internal.aliyuncs.com</IntranetEndpoint>
    --             <Location>oss-us-west-1</Location>
    --             <Name>chilkat-testbucket</Name>
    --             <Region>us-west-1</Region>
    --             <StorageClass>STANDARD</StorageClass>
    --         </Bucket>
    --         <Bucket>
    --             <Comment/>
    --             <CreationDate>2021-10-28T23:52:10.000Z</CreationDate>
    --             <ExtranetEndpoint>oss-us-west-1.aliyuncs.com</ExtranetEndpoint>
    --             <IntranetEndpoint>oss-us-west-1-internal.aliyuncs.com</IntranetEndpoint>
    --             <Location>oss-us-west-1</Location>
    --             <Name>chilkat2</Name>
    --             <Region>us-west-1</Region>
    --             <StorageClass>STANDARD</StorageClass>
    --         </Bucket>
    --     </Buckets>
    -- </ListAllMyBucketsResult>

    DECLARE @CreationDate nvarchar(4000)

    DECLARE @ExtranetEndpoint nvarchar(4000)

    DECLARE @IntranetEndpoint nvarchar(4000)

    DECLARE @Location nvarchar(4000)

    DECLARE @Name nvarchar(4000)

    DECLARE @Region nvarchar(4000)

    DECLARE @StorageClass nvarchar(4000)

    DECLARE @ListAllMyBucketsResult_xmlns nvarchar(4000)
    EXEC sp_OAMethod @xml, 'GetAttrValue', @ListAllMyBucketsResult_xmlns OUT, 'xmlns'
    DECLARE @ID nvarchar(4000)
    EXEC sp_OAMethod @xml, 'GetChildContent', @ID OUT, 'Owner|ID'
    DECLARE @DisplayName nvarchar(4000)
    EXEC sp_OAMethod @xml, 'GetChildContent', @DisplayName OUT, 'Owner|DisplayName'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @xml, 'NumChildrenHavingTag', @count_i OUT, 'Buckets|Bucket'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @xml, 'I', @i
        EXEC sp_OAMethod @xml, 'GetChildContent', @CreationDate OUT, 'Buckets|Bucket[i]|CreationDate'
        EXEC sp_OAMethod @xml, 'GetChildContent', @ExtranetEndpoint OUT, 'Buckets|Bucket[i]|ExtranetEndpoint'
        EXEC sp_OAMethod @xml, 'GetChildContent', @IntranetEndpoint OUT, 'Buckets|Bucket[i]|IntranetEndpoint'
        EXEC sp_OAMethod @xml, 'GetChildContent', @Location OUT, 'Buckets|Bucket[i]|Location'
        EXEC sp_OAMethod @xml, 'GetChildContent', @Name OUT, 'Buckets|Bucket[i]|Name'
        EXEC sp_OAMethod @xml, 'GetChildContent', @Region OUT, 'Buckets|Bucket[i]|Region'
        EXEC sp_OAMethod @xml, 'GetChildContent', @StorageClass OUT, 'Buckets|Bucket[i]|StorageClass'
        SELECT @i = @i + 1
      END

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


END
GO