SQL Server Google Cloud Storage: Create Bucket

Back to Index

Creates a new bucket.

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

CURL Command

curl -X POST https://www.googleapis.com/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT \
    --header "Authorization: Bearer CLOUD_STORAGE_TOKEN" \
    --header "Content-Type: application/json" \
    --data '{"name": "chilkat-test-bucket"}'

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?project=MY_CLOUD_STORAGE_PROJECT
    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

    --  See the Online Tool for Generating JSON Creation Code
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name', 'chilkat-test-bucket'

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Authorization', 'Bearer CLOUD_STORAGE_TOKEN'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/json'

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

    EXEC sp_OAMethod @json, 'EmitSb', @success OUT, @sbRequestBody
    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT', @sbRequestBody, @sbResponseBody
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        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 @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        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 @kind nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @kind OUT, 'kind'
    DECLARE @id nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @id OUT, 'id'
    DECLARE @selfLink nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @selfLink OUT, 'selfLink'
    DECLARE @projectNumber nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @projectNumber OUT, 'projectNumber'
    DECLARE @name nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @name OUT, 'name'
    DECLARE @timeCreated nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @timeCreated OUT, 'timeCreated'
    DECLARE @updated nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @updated OUT, 'updated'
    DECLARE @metageneration nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @metageneration OUT, 'metageneration'
    DECLARE @location nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @location OUT, 'location'
    DECLARE @storageClass nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @storageClass OUT, 'storageClass'
    DECLARE @etag nvarchar(4000)
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @etag OUT, 'etag'

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


END
GO

Sample JSON Response Body

{
  "kind": "storage#bucket",
  "id": "chilkat-test-bucket",
  "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-test-bucket",
  "projectNumber": "5332332985",
  "name": "chilkat-test-bucket",
  "timeCreated": "2018-10-23T11:52:52.464Z",
  "updated": "2018-10-23T11:52:52.464Z",
  "metageneration": "1",
  "location": "US",
  "storageClass": "STANDARD",
  "etag": "CAE="
}