PowerBuilder Google Cloud Storage: List Objects in a Bucket

Back to Index

Retrieves a list of objects in the Google Cloud Storage bucket. The name of the bucket is specified in the URL's path. This example retrieves a listing of the objects in the "chilkat-test" bucket.

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

CURL Command

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

PowerBuilder Example

integer li_rc
oleobject loo_Rest
integer li_Success
integer li_BTls
integer li_Port
integer li_BAutoReconnect
oleobject loo_SbResponseBody
integer li_RespStatusCode
oleobject loo_JsonResponse
integer i
integer li_Count_i
string ls_Kind
string ls_Id
string ls_SelfLink
string ls_Name
string ls_Bucket
string ls_Generation
string ls_Metageneration
string ls_ContentType
string ls_TimeCreated
string ls_Updated
string ls_StorageClass
string ls_TimeStorageClassUpdated
string ls_Size
string ls_Md5Hash
string ls_MediaLink
string ls_Crc32c
string ls_Etag

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat_9_5_0.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  URL: https://www.googleapis.com/storage/v1/b/chilkat-bucket/o?project=MY_CLOUD_STORAGE_PROJECT
li_BTls = 1
li_Port = 443
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("www.googleapis.com",li_Port,li_BTls,li_BAutoReconnect)
if li_Success <> 1 then
    Write-Debug "ConnectFailReason: " + string(loo_Rest.ConnectFailReason)
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

loo_Rest.AddHeader("Authorization","Bearer CLOUD_STORAGE_TOKEN")

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

li_Success = loo_Rest.FullRequestNoBodySb("GET","/storage/v1/b/chilkat-bucket/o?project=MY_CLOUD_STORAGE_PROJECT",loo_SbResponseBody)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_SbResponseBody
    return
end if

li_RespStatusCode = loo_Rest.ResponseStatusCode
if li_RespStatusCode >= 400 then
    Write-Debug "Response Status Code = " + string(li_RespStatusCode)
    Write-Debug "Response Header:"
    Write-Debug loo_Rest.ResponseHeader
    Write-Debug "Response Body:"
    Write-Debug loo_SbResponseBody.GetAsString()
    destroy loo_Rest
    destroy loo_SbResponseBody
    return
end if

loo_JsonResponse = create oleobject
li_rc = loo_JsonResponse.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_JsonResponse.LoadSb(loo_SbResponseBody)

//  See the Online Tool for Generating JSON Parse Code

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


destroy loo_Rest
destroy loo_SbResponseBody
destroy loo_JsonResponse

Sample JSON Response Body

{
  "kind": "storage#objects",
  "items": [
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/hedgehogs.jpg/1540253106638630",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/hedgehogs.jpg",
      "name": "hedgehogs.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106638630",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.638Z",
      "updated": "2018-10-23T00:05:06.638Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.638Z",
      "size": "48573",
      "md5Hash": "TE1U6fQlD6MOVGaQbwGinQ==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/hedgehogs.jpg?generation=1540253106638630&alt=media",
      "crc32c": "1B2d6g==",
      "etag": "CKaepPqhm94CEAE="
    },
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/penguins.jpg/1540253106941673",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/penguins.jpg",
      "name": "penguins.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106941673",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.941Z",
      "updated": "2018-10-23T00:05:06.941Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.941Z",
      "size": "777835",
      "md5Hash": "nTd7EM53jEk4s8fixjoimg==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/penguins.jpg?generation=1540253106941673&alt=media",
      "crc32c": "ixxYVw==",
      "etag": "COndtvqhm94CEAE="
    },
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/starfish.jpg/1540253106637454",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/starfish.jpg",
      "name": "starfish.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106637454",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.636Z",
      "updated": "2018-10-23T00:05:06.636Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.636Z",
      "size": "6229",
      "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/starfish.jpg?generation=1540253106637454&alt=media",
      "crc32c": "9RjgwQ==",
      "etag": "CI6VpPqhm94CEAE="
    }
  ]
}