Xbase++
Xbase++
Google Cloud Storage List Buckets
See more Google Cloud Storage Examples
Demonstrates how to retrieve a list of buckets for a given project.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oJsonToken
LOCAL oHttp
LOCAL oResp
LOCAL nResponseCode
LOCAL oJson
LOCAL cKind
LOCAL i
LOCAL nCount_i
LOCAL cId
LOCAL cSelfLink
LOCAL cProjectNumber
LOCAL cName
LOCAL cTimeCreated
LOCAL cUpdated
LOCAL cMetageneration
LOCAL nIamConfigurationBucketPolicyOnlyEnabled
LOCAL cLocation
LOCAL cStorageClass
LOCAL cEtag
nSuccess := 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"
oJsonToken := CreateObject("Chilkat.JsonObject")
nSuccess := oJsonToken:LoadFile("qa_data/tokens/googleCloudStorage.json")
IF (nSuccess == 0)
? oJsonToken:LastErrorText
oJsonToken:destroy()
RETURN
ENDIF
oHttp := CreateObject("Chilkat.Http")
oHttp:AuthToken := oJsonToken:StringOf("access_token")
// For more info see Cloud Storage Documentation - Buckets: list
//
nSuccess := oHttp:SetUrlVar("PROJECT_ID", "chilkattest-1050")
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpNoBody("GET", "https://www.googleapis.com/storage/v1/b?project={$PROJECT_ID}", oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oJsonToken:destroy()
oHttp:destroy()
oResp:destroy()
RETURN
ENDIF
nResponseCode := oResp:StatusCode
IF (nResponseCode == 401)
? oResp:BodyStr
? "If invalid credentials, then it is likely the access token expired."
? "Your app should automatically fetch a new access token and re-try."
oJsonToken:destroy()
oHttp:destroy()
oResp:destroy()
RETURN
ENDIF
? "Response code: " + Str(nResponseCode)
? "Response body"
oJson := CreateObject("Chilkat.JsonObject")
nSuccess := oJson:Load(oResp:BodyStr)
oJson:EmitCompact := 0
? oJson: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
cKind := oJson:StringOf("kind")
i := 0
nCount_i := oJson:SizeOfArray("items")
DO WHILE i < nCount_i
oJson:I := i
cKind := oJson:StringOf("items[i].kind")
cId := oJson:StringOf("items[i].id")
cSelfLink := oJson:StringOf("items[i].selfLink")
cProjectNumber := oJson:StringOf("items[i].projectNumber")
cName := oJson:StringOf("items[i].name")
cTimeCreated := oJson:StringOf("items[i].timeCreated")
cUpdated := oJson:StringOf("items[i].updated")
cMetageneration := oJson:StringOf("items[i].metageneration")
nIamConfigurationBucketPolicyOnlyEnabled := oJson:BoolOf("items[i].iamConfiguration.bucketPolicyOnly.enabled")
cLocation := oJson:StringOf("items[i].location")
cStorageClass := oJson:StringOf("items[i].storageClass")
cEtag := oJson:StringOf("items[i].etag")
i := i + 1
ENDDO
oJsonToken:destroy()
oHttp:destroy()
oResp:destroy()
oJson:destroy()