Unicode C++ Google Cloud Storage: List Objects in a Bucket

Back to Index

Retrieves a list of objects in the Google Cloud Storage bucket. The name of the bucket is specified in the URL's path. This example retrieves a listing of the objects in the "chilkat-test" bucket.

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

CURL Command

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

Unicode C++ Example

#include <CkRestW.h>
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>

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

    //  URL: https://www.googleapis.com/storage/v1/b/chilkat-bucket/o?project=MY_CLOUD_STORAGE_PROJECT
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect(L"www.googleapis.com",port,bTls,bAutoReconnect);
    if (success != true) {
        wprintf(L"ConnectFailReason: %d\n",rest.get_ConnectFailReason());
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

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

    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestNoBodySb(L"GET",L"/storage/v1/b/chilkat-bucket/o?project=MY_CLOUD_STORAGE_PROJECT",sbResponseBody);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();
    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",rest.responseHeader());
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",sbResponseBody.getAsString());
        return;
    }

    CkJsonObjectW jsonResponse;
    jsonResponse.LoadSb(sbResponseBody);

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

    const wchar_t *kind = jsonResponse.stringOf(L"kind");
    i = 0;
    count_i = jsonResponse.SizeOfArray(L"items");
    while (i < count_i) {
        jsonResponse.put_I(i);
        kind = jsonResponse.stringOf(L"items[i].kind");
        const wchar_t *id = jsonResponse.stringOf(L"items[i].id");
        const wchar_t *selfLink = jsonResponse.stringOf(L"items[i].selfLink");
        const wchar_t *name = jsonResponse.stringOf(L"items[i].name");
        const wchar_t *bucket = jsonResponse.stringOf(L"items[i].bucket");
        const wchar_t *generation = jsonResponse.stringOf(L"items[i].generation");
        const wchar_t *metageneration = jsonResponse.stringOf(L"items[i].metageneration");
        const wchar_t *contentType = jsonResponse.stringOf(L"items[i].contentType");
        const wchar_t *timeCreated = jsonResponse.stringOf(L"items[i].timeCreated");
        const wchar_t *updated = jsonResponse.stringOf(L"items[i].updated");
        const wchar_t *storageClass = jsonResponse.stringOf(L"items[i].storageClass");
        const wchar_t *timeStorageClassUpdated = jsonResponse.stringOf(L"items[i].timeStorageClassUpdated");
        const wchar_t *size = jsonResponse.stringOf(L"items[i].size");
        const wchar_t *md5Hash = jsonResponse.stringOf(L"items[i].md5Hash");
        const wchar_t *mediaLink = jsonResponse.stringOf(L"items[i].mediaLink");
        const wchar_t *crc32c = jsonResponse.stringOf(L"items[i].crc32c");
        const wchar_t *etag = jsonResponse.stringOf(L"items[i].etag");
        i = i + 1;
    }
    }

Sample JSON Response Body

{
  "kind": "storage#objects",
  "items": [
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/hedgehogs.jpg/1540253106638630",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/hedgehogs.jpg",
      "name": "hedgehogs.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106638630",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.638Z",
      "updated": "2018-10-23T00:05:06.638Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.638Z",
      "size": "48573",
      "md5Hash": "TE1U6fQlD6MOVGaQbwGinQ==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/hedgehogs.jpg?generation=1540253106638630&alt=media",
      "crc32c": "1B2d6g==",
      "etag": "CKaepPqhm94CEAE="
    },
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/penguins.jpg/1540253106941673",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/penguins.jpg",
      "name": "penguins.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106941673",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.941Z",
      "updated": "2018-10-23T00:05:06.941Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.941Z",
      "size": "777835",
      "md5Hash": "nTd7EM53jEk4s8fixjoimg==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/penguins.jpg?generation=1540253106941673&alt=media",
      "crc32c": "ixxYVw==",
      "etag": "COndtvqhm94CEAE="
    },
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/starfish.jpg/1540253106637454",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/starfish.jpg",
      "name": "starfish.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106637454",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.636Z",
      "updated": "2018-10-23T00:05:06.636Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.636Z",
      "size": "6229",
      "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/starfish.jpg?generation=1540253106637454&alt=media",
      "crc32c": "9RjgwQ==",
      "etag": "CI6VpPqhm94CEAE="
    }
  ]
}