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

Unicode C Example

#include <C_CkRestW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkStringBuilderW sbResponseBody;
    int respStatusCode;
    HCkJsonObjectW jsonResponse;
    int i;
    int count_i;
    const wchar_t *kind;
    const wchar_t *id;
    const wchar_t *selfLink;
    const wchar_t *projectNumber;
    const wchar_t *name;
    const wchar_t *timeCreated;
    const wchar_t *updated;
    const wchar_t *metageneration;
    const wchar_t *location;
    const wchar_t *storageClass;
    const wchar_t *etag;

    rest = CkRestW_Create();

    //  URL: https://www.googleapis.com/storage/v1/b
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"www.googleapis.com",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        wprintf(L"ConnectFailReason: %d\n",CkRestW_getConnectFailReason(rest));
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

    CkRestW_AddQueryParam(rest,L"project",L"MY_CLOUD_STORAGE_PROJECT");

    CkRestW_AddHeader(rest,L"Authorization",L"Bearer CLOUD_STORAGE_TOKEN");

    sbResponseBody = CkStringBuilderW_Create();
    success = CkRestW_FullRequestNoBodySb(rest,L"GET",L"/storage/v1/b",sbResponseBody);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    respStatusCode = CkRestW_getResponseStatusCode(rest);
    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkRestW_responseHeader(rest));
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponseBody));
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    jsonResponse = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jsonResponse,sbResponseBody);

    //  See the Online Tool for Generating JSON Parse Code

    kind = CkJsonObjectW_stringOf(jsonResponse,L"kind");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"items");
    while (i < count_i) {
        CkJsonObjectW_putI(jsonResponse,i);
        kind = CkJsonObjectW_stringOf(jsonResponse,L"items[i].kind");
        id = CkJsonObjectW_stringOf(jsonResponse,L"items[i].id");
        selfLink = CkJsonObjectW_stringOf(jsonResponse,L"items[i].selfLink");
        projectNumber = CkJsonObjectW_stringOf(jsonResponse,L"items[i].projectNumber");
        name = CkJsonObjectW_stringOf(jsonResponse,L"items[i].name");
        timeCreated = CkJsonObjectW_stringOf(jsonResponse,L"items[i].timeCreated");
        updated = CkJsonObjectW_stringOf(jsonResponse,L"items[i].updated");
        metageneration = CkJsonObjectW_stringOf(jsonResponse,L"items[i].metageneration");
        location = CkJsonObjectW_stringOf(jsonResponse,L"items[i].location");
        storageClass = CkJsonObjectW_stringOf(jsonResponse,L"items[i].storageClass");
        etag = CkJsonObjectW_stringOf(jsonResponse,L"items[i].etag");
        i = i + 1;
    }



    CkRestW_Dispose(rest);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jsonResponse);

    }

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="
    }
  ]
}