SQL Server
SQL Server
Google Cloud Storage List Bucket Objects
See more Google Cloud Storage Examples
List the objects in a Google Cloud Storage bucket.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr 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.
-- This example uses a previously obtained access token having permission for the
-- scope "https://www.googleapis.com/auth/cloud-platform"
-- In this example, Get Google Cloud Storage OAuth2 Access Token,
-- the service account access token was saved to a text file. This example fetches the access token from the file..
DECLARE @sbToken int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbToken OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @sbToken, 'LoadFile', @success OUT, 'qa_data/tokens/googleCloudStorageAccessToken.txt', 'utf-8'
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OAMethod @sbToken, 'GetAsString', @sTmp0 OUT
EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
-- For more info see Cloud Storage Documentation - Objects: list
--
EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'bucket', 'chilkat-ocean'
EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'PROJECT_NAME', 'ChilkatCloud'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', 'https://www.googleapis.com/storage/v1/b/{$bucket}/o?project={$ChilkatCloud}', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbToken
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @responseCode int
EXEC sp_OAGetProperty @resp, 'StatusCode', @responseCode OUT
IF @responseCode = 401
BEGIN
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
PRINT 'If invalid credentials, then it is likely the access token expired.'
PRINT 'Your app should automatically fetch a new access token and re-try.'
EXEC @hr = sp_OADestroy @sbToken
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
RETURN
END
PRINT 'Response code: ' + @responseCode
PRINT 'Response body'
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @json, 'Load', @success OUT, @sTmp0
EXEC sp_OASetProperty @json, 'EmitCompact', 0
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
-- A response code = 200 indicates success, and the response body contains JSON such as this:
-- {
-- "kind": "storage#objects",
-- "items": [
-- {
-- "kind": "storage#object",
-- "id": "chilkat-ocean/file with space char.jpg/1555776690752366",
-- "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg",
-- "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg?generation=1555776690752366&alt=media",
-- "name": "file with space char.jpg",
-- "bucket": "chilkat-ocean",
-- "generation": "1555776690752366",
-- "metageneration": "1",
-- "contentType": "image/jpeg",
-- "storageClass": "MULTI_REGIONAL",
-- "size": "6229",
-- "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
-- "crc32c": "9RjgwQ==",
-- "etag": "CO6WguiH3+ECEAE=",
-- "timeCreated": "2019-04-20T16:11:30.752Z",
-- "updated": "2019-04-20T16:11:30.752Z",
-- "timeStorageClassUpdated": "2019-04-20T16:11:30.752Z"
-- },
-- {
-- "kind": "storage#object",
--
-- "id": "chilkat-ocean/penguins.jpg/1555775749593296",
-- "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/penguins.jpg",
-- "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/penguins.jpg?generation=1555775749593296&alt=media",
-- "name": "penguins.jpg",
-- "bucket": "chilkat-ocean",
-- "generation": "1555775749593296",
-- "metageneration": "1",
-- "contentType": "image/jpeg",
-- "storageClass": "MULTI_REGIONAL",
-- "size": "777835",
-- "md5Hash": "nTd7EM53jEk4s8fixjoimg==",
-- "crc32c": "ixxYVw==",
-- "etag": "CNCxnqeE3+ECEAE=",
-- "timeCreated": "2019-04-20T15:55:49.593Z",
-- "updated": "2019-04-20T15:55:49.593Z",
-- "timeStorageClassUpdated": "2019-04-20T15:55:49.593Z"
-- },
-- {
-- "kind": "storage#object",
-- "id": "chilkat-ocean/starfish.jpg/1628958192639968",
-- "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/starfish.jpg",
-- "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/starfish.jpg?generation=1628958192639968&alt=media",
-- "name": "starfish.jpg",
-- "bucket": "chilkat-ocean",
-- "generation": "1628958192639968",
-- "metageneration": "1",
-- "contentType": "image/jpeg",
-- "storageClass": "MULTI_REGIONAL",
-- "size": "6229",
-- "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
-- "crc32c": "9RjgwQ==",
-- "etag": "COC/tJP2sPICEAE=",
-- "timeCreated": "2021-08-14T16:23:12.660Z",
-- "updated": "2021-08-14T16:23:12.660Z",
-- "timeStorageClassUpdated": "2021-08-14T16:23:12.660Z"
-- }
-- ]
-- }
-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
DECLARE @id nvarchar(4000)
DECLARE @selfLink nvarchar(4000)
DECLARE @mediaLink nvarchar(4000)
DECLARE @name nvarchar(4000)
DECLARE @bucket nvarchar(4000)
DECLARE @generation nvarchar(4000)
DECLARE @metageneration nvarchar(4000)
DECLARE @contentType nvarchar(4000)
DECLARE @storageClass nvarchar(4000)
DECLARE @size nvarchar(4000)
DECLARE @md5Hash nvarchar(4000)
DECLARE @crc32c nvarchar(4000)
DECLARE @etag nvarchar(4000)
DECLARE @timeCreated nvarchar(4000)
DECLARE @updated nvarchar(4000)
DECLARE @timeStorageClassUpdated nvarchar(4000)
DECLARE @kind nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @kind OUT, 'kind'
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'items'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @json, 'I', @i
EXEC sp_OAMethod @json, 'StringOf', @kind OUT, 'items[i].kind'
EXEC sp_OAMethod @json, 'StringOf', @id OUT, 'items[i].id'
EXEC sp_OAMethod @json, 'StringOf', @selfLink OUT, 'items[i].selfLink'
EXEC sp_OAMethod @json, 'StringOf', @mediaLink OUT, 'items[i].mediaLink'
EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'items[i].name'
EXEC sp_OAMethod @json, 'StringOf', @bucket OUT, 'items[i].bucket'
EXEC sp_OAMethod @json, 'StringOf', @generation OUT, 'items[i].generation'
EXEC sp_OAMethod @json, 'StringOf', @metageneration OUT, 'items[i].metageneration'
EXEC sp_OAMethod @json, 'StringOf', @contentType OUT, 'items[i].contentType'
EXEC sp_OAMethod @json, 'StringOf', @storageClass OUT, 'items[i].storageClass'
EXEC sp_OAMethod @json, 'StringOf', @size OUT, 'items[i].size'
EXEC sp_OAMethod @json, 'StringOf', @md5Hash OUT, 'items[i].md5Hash'
EXEC sp_OAMethod @json, 'StringOf', @crc32c OUT, 'items[i].crc32c'
EXEC sp_OAMethod @json, 'StringOf', @etag OUT, 'items[i].etag'
EXEC sp_OAMethod @json, 'StringOf', @timeCreated OUT, 'items[i].timeCreated'
EXEC sp_OAMethod @json, 'StringOf', @updated OUT, 'items[i].updated'
EXEC sp_OAMethod @json, 'StringOf', @timeStorageClassUpdated OUT, 'items[i].timeStorageClassUpdated'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @sbToken
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @json
END
GO