SQL Server Google Cloud Storage: List Buckets

Back to Index

Retrieves a list of buckets for a given project.

Documentation: https://cloud.google.com/storage/docs/json_api/v1/buckets/list

CURL Command

curl -X GET https://www.googleapis.com/storage/v1/b \
    --header "Authorization: Bearer CLOUD_STORAGE_TOKEN" \
    --data "project=MY_CLOUD_STORAGE_PROJECT"

SQL Server Example

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    --  URL: https://www.googleapis.com/storage/v1/b
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'www.googleapis.com', @port, @bTls, @bAutoReconnect
    IF @success <> 1
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ConnectFailReason', @iTmp0 OUT
        PRINT 'ConnectFailReason: ' + @iTmp0
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'project', 'MY_CLOUD_STORAGE_PROJECT'

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Authorization', 'Bearer CLOUD_STORAGE_TOKEN'

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/storage/v1/b', @sbResponseBody
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END
    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @respStatusCode OUT
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Status Code = ' + @respStatusCode

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Response Body:'
        EXEC sp_OAMethod @sbResponseBody, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jsonResponse int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonResponse OUT

    EXEC sp_OAMethod @jsonResponse, 'LoadSb', @success OUT, @sbResponseBody

    --  See the Online Tool for Generating JSON Parse Code
    DECLARE @i int

    DECLARE @count_i int

    DECLARE @kind nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @kind OUT, 'kind'
    SELECT @i = 0
    EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_i OUT, 'items'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jsonResponse, 'I', @i
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @kind OUT, 'items[i].kind'
        DECLARE @id nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @id OUT, 'items[i].id'
        DECLARE @selfLink nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @selfLink OUT, 'items[i].selfLink'
        DECLARE @projectNumber nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @projectNumber OUT, 'items[i].projectNumber'
        DECLARE @name nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @name OUT, 'items[i].name'
        DECLARE @timeCreated nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @timeCreated OUT, 'items[i].timeCreated'
        DECLARE @updated nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @updated OUT, 'items[i].updated'
        DECLARE @metageneration nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @metageneration OUT, 'items[i].metageneration'
        DECLARE @location nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @location OUT, 'items[i].location'
        DECLARE @storageClass nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @storageClass OUT, 'items[i].storageClass'
        DECLARE @etag nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @etag OUT, 'items[i].etag'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jsonResponse


END
GO

Sample JSON Response Body

{
  "kind": "storage#buckets",
  "items": [
    {
      "kind": "storage#bucket",
      "id": "chilkat-bucket",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket",
      "projectNumber": "5332332985",
      "name": "chilkat-bucket",
      "timeCreated": "2018-10-23T00:04:44.507Z",
      "updated": "2018-10-23T00:04:44.507Z",
      "metageneration": "1",
      "location": "US",
      "storageClass": "MULTI_REGIONAL",
      "etag": "CAE="
    },
    {
      "kind": "storage#bucket",
      "id": "chilkat-images",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-images",
      "projectNumber": "5332332985",
      "name": "chilkat-images",
      "timeCreated": "2018-10-23T11:24:43.000Z",
      "updated": "2018-10-23T11:24:43.000Z",
      "metageneration": "1",
      "location": "US",
      "storageClass": "MULTI_REGIONAL",
      "etag": "CAE="
    }
  ]
}