Sample code for 30+ languages & platforms
Unicode C++

Google Cloud Storage List Buckets

See more Google Cloud Storage Examples

Demonstrates how to retrieve a list of buckets for a given project.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkJsonObjectW jsonToken;
    success = jsonToken.LoadFile(L"qa_data/tokens/googleCloudStorage.json");
    if (success == false) {
        wprintf(L"%s\n",jsonToken.lastErrorText());
        return;
    }

    CkHttpW http;
    http.put_AuthToken(jsonToken.stringOf(L"access_token"));

    // For more info see Cloud Storage Documentation - Buckets: list 
    // 
    success = http.SetUrlVar(L"PROJECT_ID",L"chilkattest-1050");
    CkHttpResponseW resp;
    success = http.HttpNoBody(L"GET",L"https://www.googleapis.com/storage/v1/b?project={$PROJECT_ID}",resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    int responseCode = resp.get_StatusCode();
    if (responseCode == 401) {
        wprintf(L"%s\n",resp.bodyStr());
        wprintf(L"If invalid credentials, then it is likely the access token expired.\n");
        wprintf(L"Your app should automatically fetch a new access token and re-try.\n");
        return;
    }

    wprintf(L"Response code: %d\n",responseCode);
    wprintf(L"Response body\n");

    CkJsonObjectW json;
    success = json.Load(resp.bodyStr());

    json.put_EmitCompact(false);

    wprintf(L"%s\n",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

    const wchar_t *kind = 0;
    int i;
    int count_i;
    const wchar_t *id = 0;
    const wchar_t *selfLink = 0;
    const wchar_t *projectNumber = 0;
    const wchar_t *name = 0;
    const wchar_t *timeCreated = 0;
    const wchar_t *updated = 0;
    const wchar_t *metageneration = 0;
    bool iamConfigurationBucketPolicyOnlyEnabled;
    const wchar_t *location = 0;
    const wchar_t *storageClass = 0;
    const wchar_t *etag = 0;

    kind = json.stringOf(L"kind");
    i = 0;
    count_i = json.SizeOfArray(L"items");
    while (i < count_i) {
        json.put_I(i);
        kind = json.stringOf(L"items[i].kind");
        id = json.stringOf(L"items[i].id");
        selfLink = json.stringOf(L"items[i].selfLink");
        projectNumber = json.stringOf(L"items[i].projectNumber");
        name = json.stringOf(L"items[i].name");
        timeCreated = json.stringOf(L"items[i].timeCreated");
        updated = json.stringOf(L"items[i].updated");
        metageneration = json.stringOf(L"items[i].metageneration");
        iamConfigurationBucketPolicyOnlyEnabled = json.BoolOf(L"items[i].iamConfiguration.bucketPolicyOnly.enabled");
        location = json.stringOf(L"items[i].location");
        storageClass = json.stringOf(L"items[i].storageClass");
        etag = json.stringOf(L"items[i].etag");
        i = i + 1;
    }
    }