C++ Google Cloud Storage: List Buckets

Back to Index

Retrieves a list of buckets for a given project.

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

CURL Command

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

C++ Example

#include <CkRest.h>
#include <CkStringBuilder.h>
#include <CkJsonObject.h>

void ChilkatSample(void)
    {
    CkRest rest;
    bool success;

    //  URL: https://www.googleapis.com/storage/v1/b
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect("www.googleapis.com",port,bTls,bAutoReconnect);
    if (success != true) {
        std::cout << "ConnectFailReason: " << rest.get_ConnectFailReason() << "\r\n";
        std::cout << rest.lastErrorText() << "\r\n";
        return;
    }

    rest.AddQueryParam("project","MY_CLOUD_STORAGE_PROJECT");

    rest.AddHeader("Authorization","Bearer CLOUD_STORAGE_TOKEN");

    CkStringBuilder sbResponseBody;
    success = rest.FullRequestNoBodySb("GET","/storage/v1/b",sbResponseBody);
    if (success != true) {
        std::cout << rest.lastErrorText() << "\r\n";
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();
    if (respStatusCode >= 400) {
        std::cout << "Response Status Code = " << respStatusCode << "\r\n";
        std::cout << "Response Header:" << "\r\n";
        std::cout << rest.responseHeader() << "\r\n";
        std::cout << "Response Body:" << "\r\n";
        std::cout << sbResponseBody.getAsString() << "\r\n";
        return;
    }

    CkJsonObject jsonResponse;
    jsonResponse.LoadSb(sbResponseBody);

    //  See the Online Tool for Generating JSON Parse Code
    int i;
    int count_i;

    const char *kind = jsonResponse.stringOf("kind");
    i = 0;
    count_i = jsonResponse.SizeOfArray("items");
    while (i < count_i) {
        jsonResponse.put_I(i);
        kind = jsonResponse.stringOf("items[i].kind");
        const char *id = jsonResponse.stringOf("items[i].id");
        const char *selfLink = jsonResponse.stringOf("items[i].selfLink");
        const char *projectNumber = jsonResponse.stringOf("items[i].projectNumber");
        const char *name = jsonResponse.stringOf("items[i].name");
        const char *timeCreated = jsonResponse.stringOf("items[i].timeCreated");
        const char *updated = jsonResponse.stringOf("items[i].updated");
        const char *metageneration = jsonResponse.stringOf("items[i].metageneration");
        const char *location = jsonResponse.stringOf("items[i].location");
        const char *storageClass = jsonResponse.stringOf("items[i].storageClass");
        const char *etag = jsonResponse.stringOf("items[i].etag");
        i = i + 1;
    }
    }

Sample JSON Response Body

{
  "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",
      "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",
      "location": "US",
      "storageClass": "MULTI_REGIONAL",
      "etag": "CAE="
    }
  ]
}