PowerBuilder
PowerBuilder
Google Cloud Storage List Buckets
See more Google Cloud Storage Examples
Demonstrates how to retrieve a list of buckets for a given project.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_JsonToken
oleobject loo_Http
oleobject loo_Resp
integer li_ResponseCode
oleobject loo_Json
string ls_Kind
integer i
integer li_Count_i
string ls_Id
string ls_SelfLink
string ls_ProjectNumber
string ls_Name
string ls_TimeCreated
string ls_Updated
string ls_Metageneration
integer li_IamConfigurationBucketPolicyOnlyEnabled
string ls_Location
string ls_StorageClass
string ls_Etag
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"
loo_JsonToken = create oleobject
li_rc = loo_JsonToken.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_JsonToken
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_JsonToken.LoadFile("qa_data/tokens/googleCloudStorage.json")
if li_Success = 0 then
Write-Debug loo_JsonToken.LastErrorText
destroy loo_JsonToken
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.AuthToken = loo_JsonToken.StringOf("access_token")
// For more info see Cloud Storage Documentation - Buckets: list
//
li_Success = loo_Http.SetUrlVar("PROJECT_ID","chilkattest-1050")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpNoBody("GET","https://www.googleapis.com/storage/v1/b?project={$PROJECT_ID}",loo_Resp)
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_JsonToken
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_JsonToken
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#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",
// "iamConfiguration": {
// "bucketPolicyOnly": {
// "enabled": false
// }
// },
// "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",
// "iamConfiguration": {
// "bucketPolicyOnly": {
// "enabled": false
// }
// },
// "location": "US",
// "storageClass": "MULTI_REGIONAL",
// "etag": "CAE="
// }
// ]
// }
// 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_ProjectNumber = loo_Json.StringOf("items[i].projectNumber")
ls_Name = loo_Json.StringOf("items[i].name")
ls_TimeCreated = loo_Json.StringOf("items[i].timeCreated")
ls_Updated = loo_Json.StringOf("items[i].updated")
ls_Metageneration = loo_Json.StringOf("items[i].metageneration")
li_IamConfigurationBucketPolicyOnlyEnabled = loo_Json.BoolOf("items[i].iamConfiguration.bucketPolicyOnly.enabled")
ls_Location = loo_Json.StringOf("items[i].location")
ls_StorageClass = loo_Json.StringOf("items[i].storageClass")
ls_Etag = loo_Json.StringOf("items[i].etag")
i = i + 1
loop
destroy loo_JsonToken
destroy loo_Http
destroy loo_Resp
destroy loo_Json