Unicode C
Unicode C
Google Cloud Storage List Bucket Objects
See more Google Cloud Storage Examples
List the objects in a Google Cloud Storage bucket.Chilkat Unicode C Downloads
#include <C_CkStringBuilderW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkStringBuilderW sbToken;
HCkHttpW http;
HCkHttpResponseW resp;
int responseCode;
HCkJsonObjectW json;
const wchar_t *id;
const wchar_t *selfLink;
const wchar_t *mediaLink;
const wchar_t *name;
const wchar_t *bucket;
const wchar_t *generation;
const wchar_t *metageneration;
const wchar_t *contentType;
const wchar_t *storageClass;
const wchar_t *size;
const wchar_t *md5Hash;
const wchar_t *crc32c;
const wchar_t *etag;
const wchar_t *timeCreated;
const wchar_t *updated;
const wchar_t *timeStorageClassUpdated;
const wchar_t *kind;
int i;
int count_i;
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"
// In this example, Get Google Cloud Storage OAuth2 Access Token,
// the service account access token was saved to a text file. This example fetches the access token from the file..
sbToken = CkStringBuilderW_Create();
CkStringBuilderW_LoadFile(sbToken,L"qa_data/tokens/googleCloudStorageAccessToken.txt",L"utf-8");
http = CkHttpW_Create();
CkHttpW_putAuthToken(http,CkStringBuilderW_getAsString(sbToken));
// For more info see Cloud Storage Documentation - Objects: list
//
success = CkHttpW_SetUrlVar(http,L"bucket",L"chilkat-ocean");
success = CkHttpW_SetUrlVar(http,L"PROJECT_NAME",L"ChilkatCloud");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpNoBody(http,L"GET",L"https://www.googleapis.com/storage/v1/b/{$bucket}/o?project={$ChilkatCloud}",resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkStringBuilderW_Dispose(sbToken);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
return;
}
responseCode = CkHttpResponseW_getStatusCode(resp);
if (responseCode == 401) {
wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
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");
CkStringBuilderW_Dispose(sbToken);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
return;
}
wprintf(L"Response code: %d\n",responseCode);
wprintf(L"Response body\n");
json = CkJsonObjectW_Create();
success = CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp));
CkJsonObjectW_putEmitCompact(json,FALSE);
wprintf(L"%s\n",CkJsonObjectW_emit(json));
// A response code = 200 indicates success, and the response body contains JSON such as this:
// {
// "kind": "storage#objects",
// "items": [
// {
// "kind": "storage#object",
// "id": "chilkat-ocean/file with space char.jpg/1555776690752366",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg",
// "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg?generation=1555776690752366&alt=media",
// "name": "file with space char.jpg",
// "bucket": "chilkat-ocean",
// "generation": "1555776690752366",
// "metageneration": "1",
// "contentType": "image/jpeg",
// "storageClass": "MULTI_REGIONAL",
// "size": "6229",
// "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
// "crc32c": "9RjgwQ==",
// "etag": "CO6WguiH3+ECEAE=",
// "timeCreated": "2019-04-20T16:11:30.752Z",
// "updated": "2019-04-20T16:11:30.752Z",
// "timeStorageClassUpdated": "2019-04-20T16:11:30.752Z"
// },
// {
// "kind": "storage#object",
//
// "id": "chilkat-ocean/penguins.jpg/1555775749593296",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/penguins.jpg",
// "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/penguins.jpg?generation=1555775749593296&alt=media",
// "name": "penguins.jpg",
// "bucket": "chilkat-ocean",
// "generation": "1555775749593296",
// "metageneration": "1",
// "contentType": "image/jpeg",
// "storageClass": "MULTI_REGIONAL",
// "size": "777835",
// "md5Hash": "nTd7EM53jEk4s8fixjoimg==",
// "crc32c": "ixxYVw==",
// "etag": "CNCxnqeE3+ECEAE=",
// "timeCreated": "2019-04-20T15:55:49.593Z",
// "updated": "2019-04-20T15:55:49.593Z",
// "timeStorageClassUpdated": "2019-04-20T15:55:49.593Z"
// },
// {
// "kind": "storage#object",
// "id": "chilkat-ocean/starfish.jpg/1628958192639968",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/starfish.jpg",
// "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/starfish.jpg?generation=1628958192639968&alt=media",
// "name": "starfish.jpg",
// "bucket": "chilkat-ocean",
// "generation": "1628958192639968",
// "metageneration": "1",
// "contentType": "image/jpeg",
// "storageClass": "MULTI_REGIONAL",
// "size": "6229",
// "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
// "crc32c": "9RjgwQ==",
// "etag": "COC/tJP2sPICEAE=",
// "timeCreated": "2021-08-14T16:23:12.660Z",
// "updated": "2021-08-14T16:23:12.660Z",
// "timeStorageClassUpdated": "2021-08-14T16:23:12.660Z"
// }
// ]
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
// See this example explaining how this memory should be used: const char * functions.
kind = CkJsonObjectW_stringOf(json,L"kind");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(json,L"items");
while (i < count_i) {
CkJsonObjectW_putI(json,i);
kind = CkJsonObjectW_stringOf(json,L"items[i].kind");
id = CkJsonObjectW_stringOf(json,L"items[i].id");
selfLink = CkJsonObjectW_stringOf(json,L"items[i].selfLink");
mediaLink = CkJsonObjectW_stringOf(json,L"items[i].mediaLink");
name = CkJsonObjectW_stringOf(json,L"items[i].name");
bucket = CkJsonObjectW_stringOf(json,L"items[i].bucket");
generation = CkJsonObjectW_stringOf(json,L"items[i].generation");
metageneration = CkJsonObjectW_stringOf(json,L"items[i].metageneration");
contentType = CkJsonObjectW_stringOf(json,L"items[i].contentType");
storageClass = CkJsonObjectW_stringOf(json,L"items[i].storageClass");
size = CkJsonObjectW_stringOf(json,L"items[i].size");
md5Hash = CkJsonObjectW_stringOf(json,L"items[i].md5Hash");
crc32c = CkJsonObjectW_stringOf(json,L"items[i].crc32c");
etag = CkJsonObjectW_stringOf(json,L"items[i].etag");
timeCreated = CkJsonObjectW_stringOf(json,L"items[i].timeCreated");
updated = CkJsonObjectW_stringOf(json,L"items[i].updated");
timeStorageClassUpdated = CkJsonObjectW_stringOf(json,L"items[i].timeStorageClassUpdated");
i = i + 1;
}
CkStringBuilderW_Dispose(sbToken);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(json);
}