PureBasic
PureBasic
Google Cloud Storage List Buckets
See more Google Cloud Storage Examples
Demonstrates how to retrieve a list of buckets for a given project.Chilkat PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 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"
jsonToken.i = CkJsonObject::ckCreate()
If jsonToken.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/googleCloudStorage.json")
If success = 0
Debug CkJsonObject::ckLastErrorText(jsonToken)
CkJsonObject::ckDispose(jsonToken)
ProcedureReturn
EndIf
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(jsonToken,"access_token"))
; For more info see Cloud Storage Documentation - Buckets: list
;
success = CkHttp::ckSetUrlVar(http,"PROJECT_ID","chilkattest-1050")
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpNoBody(http,"GET","https://www.googleapis.com/storage/v1/b?project={$PROJECT_ID}",resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkJsonObject::ckDispose(jsonToken)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
responseCode.i = CkHttpResponse::ckStatusCode(resp)
If responseCode = 401
Debug CkHttpResponse::ckBodyStr(resp)
Debug "If invalid credentials, then it is likely the access token expired."
Debug "Your app should automatically fetch a new access token and re-try."
CkJsonObject::ckDispose(jsonToken)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
Debug "Response code: " + Str(responseCode)
Debug "Response body"
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(resp))
CkJsonObject::setCkEmitCompact(json, 0)
Debug CkJsonObject::ckEmit(json)
; 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
kind.s
i.i
count_i.i
id.s
selfLink.s
projectNumber.s
name.s
timeCreated.s
updated.s
metageneration.s
iamConfigurationBucketPolicyOnlyEnabled.i
location.s
storageClass.s
etag.s
kind = CkJsonObject::ckStringOf(json,"kind")
i = 0
count_i = CkJsonObject::ckSizeOfArray(json,"items")
While i < count_i
CkJsonObject::setCkI(json, i)
kind = CkJsonObject::ckStringOf(json,"items[i].kind")
id = CkJsonObject::ckStringOf(json,"items[i].id")
selfLink = CkJsonObject::ckStringOf(json,"items[i].selfLink")
projectNumber = CkJsonObject::ckStringOf(json,"items[i].projectNumber")
name = CkJsonObject::ckStringOf(json,"items[i].name")
timeCreated = CkJsonObject::ckStringOf(json,"items[i].timeCreated")
updated = CkJsonObject::ckStringOf(json,"items[i].updated")
metageneration = CkJsonObject::ckStringOf(json,"items[i].metageneration")
iamConfigurationBucketPolicyOnlyEnabled = CkJsonObject::ckBoolOf(json,"items[i].iamConfiguration.bucketPolicyOnly.enabled")
location = CkJsonObject::ckStringOf(json,"items[i].location")
storageClass = CkJsonObject::ckStringOf(json,"items[i].storageClass")
etag = CkJsonObject::ckStringOf(json,"items[i].etag")
i = i + 1
Wend
CkJsonObject::ckDispose(jsonToken)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndProcedure