Sample code for 30+ languages & platforms
PowerBuilder

Google Cloud Storage List Bucket Objects

See more Google Cloud Storage Examples

List the objects in a Google Cloud Storage bucket.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_SbToken
oleobject loo_Http
oleobject loo_Resp
integer li_ResponseCode
oleobject loo_Json
string ls_Id
string ls_SelfLink
string ls_MediaLink
string ls_Name
string ls_Bucket
string ls_Generation
string ls_Metageneration
string ls_ContentType
string ls_StorageClass
string ls_Size
string ls_Md5Hash
string ls_Crc32c
string ls_Etag
string ls_TimeCreated
string ls_Updated
string ls_TimeStorageClassUpdated
string ls_Kind
integer i
integer li_Count_i

li_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..
loo_SbToken = create oleobject
li_rc = loo_SbToken.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_SbToken
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_SbToken.LoadFile("qa_data/tokens/googleCloudStorageAccessToken.txt","utf-8")

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.AuthToken = loo_SbToken.GetAsString()

// For more info see Cloud Storage Documentation - Objects: list 
// 
li_Success = loo_Http.SetUrlVar("bucket","chilkat-ocean")
li_Success = loo_Http.SetUrlVar("PROJECT_NAME","ChilkatCloud")

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpNoBody("GET","https://www.googleapis.com/storage/v1/b/{$bucket}/o?project={$ChilkatCloud}",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_SbToken
    destroy loo_Http
    destroy loo_Resp
    return
end if

li_ResponseCode = loo_Resp.StatusCode
if li_ResponseCode = 401 then
    Write-Debug loo_Resp.BodyStr
    Write-Debug "If invalid credentials, then it is likely the access token expired."
    Write-Debug "Your app should automatically fetch a new access token and re-try."
    destroy loo_SbToken
    destroy loo_Http
    destroy loo_Resp
    return
end if

Write-Debug "Response code: " + string(li_ResponseCode)
Write-Debug "Response body"

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Json.Load(loo_Resp.BodyStr)

loo_Json.EmitCompact = 0

Write-Debug loo_Json.Emit()

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

ls_Kind = loo_Json.StringOf("kind")
i = 0
li_Count_i = loo_Json.SizeOfArray("items")
do while i < li_Count_i
    loo_Json.I = i
    ls_Kind = loo_Json.StringOf("items[i].kind")
    ls_Id = loo_Json.StringOf("items[i].id")
    ls_SelfLink = loo_Json.StringOf("items[i].selfLink")
    ls_MediaLink = loo_Json.StringOf("items[i].mediaLink")
    ls_Name = loo_Json.StringOf("items[i].name")
    ls_Bucket = loo_Json.StringOf("items[i].bucket")
    ls_Generation = loo_Json.StringOf("items[i].generation")
    ls_Metageneration = loo_Json.StringOf("items[i].metageneration")
    ls_ContentType = loo_Json.StringOf("items[i].contentType")
    ls_StorageClass = loo_Json.StringOf("items[i].storageClass")
    ls_Size = loo_Json.StringOf("items[i].size")
    ls_Md5Hash = loo_Json.StringOf("items[i].md5Hash")
    ls_Crc32c = loo_Json.StringOf("items[i].crc32c")
    ls_Etag = loo_Json.StringOf("items[i].etag")
    ls_TimeCreated = loo_Json.StringOf("items[i].timeCreated")
    ls_Updated = loo_Json.StringOf("items[i].updated")
    ls_TimeStorageClassUpdated = loo_Json.StringOf("items[i].timeStorageClassUpdated")
    i = i + 1
loop


destroy loo_SbToken
destroy loo_Http
destroy loo_Resp
destroy loo_Json