C
C
Google Cloud Storage List Bucket Objects
See more Google Cloud Storage Examples
List the objects in a Google Cloud Storage bucket.Chilkat C Downloads
#include <C_CkStringBuilder.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkStringBuilder sbToken;
HCkHttp http;
HCkHttpResponse resp;
int responseCode;
HCkJsonObject json;
const char *id;
const char *selfLink;
const char *mediaLink;
const char *name;
const char *bucket;
const char *generation;
const char *metageneration;
const char *contentType;
const char *storageClass;
const char *size;
const char *md5Hash;
const char *crc32c;
const char *etag;
const char *timeCreated;
const char *updated;
const char *timeStorageClassUpdated;
const char *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 = CkStringBuilder_Create();
CkStringBuilder_LoadFile(sbToken,"qa_data/tokens/googleCloudStorageAccessToken.txt","utf-8");
http = CkHttp_Create();
CkHttp_putAuthToken(http,CkStringBuilder_getAsString(sbToken));
// For more info see Cloud Storage Documentation - Objects: list
//
success = CkHttp_SetUrlVar(http,"bucket","chilkat-ocean");
success = CkHttp_SetUrlVar(http,"PROJECT_NAME","ChilkatCloud");
resp = CkHttpResponse_Create();
success = CkHttp_HttpNoBody(http,"GET","https://www.googleapis.com/storage/v1/b/{$bucket}/o?project={$ChilkatCloud}",resp);
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkStringBuilder_Dispose(sbToken);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
return;
}
responseCode = CkHttpResponse_getStatusCode(resp);
if (responseCode == 401) {
printf("%s\n",CkHttpResponse_bodyStr(resp));
printf("If invalid credentials, then it is likely the access token expired.\n");
printf("Your app should automatically fetch a new access token and re-try.\n");
CkStringBuilder_Dispose(sbToken);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
return;
}
printf("Response code: %d\n",responseCode);
printf("Response body\n");
json = CkJsonObject_Create();
success = CkJsonObject_Load(json,CkHttpResponse_bodyStr(resp));
CkJsonObject_putEmitCompact(json,FALSE);
printf("%s\n",CkJsonObject_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 = CkJsonObject_stringOf(json,"kind");
i = 0;
count_i = CkJsonObject_SizeOfArray(json,"items");
while (i < count_i) {
CkJsonObject_putI(json,i);
kind = CkJsonObject_stringOf(json,"items[i].kind");
id = CkJsonObject_stringOf(json,"items[i].id");
selfLink = CkJsonObject_stringOf(json,"items[i].selfLink");
mediaLink = CkJsonObject_stringOf(json,"items[i].mediaLink");
name = CkJsonObject_stringOf(json,"items[i].name");
bucket = CkJsonObject_stringOf(json,"items[i].bucket");
generation = CkJsonObject_stringOf(json,"items[i].generation");
metageneration = CkJsonObject_stringOf(json,"items[i].metageneration");
contentType = CkJsonObject_stringOf(json,"items[i].contentType");
storageClass = CkJsonObject_stringOf(json,"items[i].storageClass");
size = CkJsonObject_stringOf(json,"items[i].size");
md5Hash = CkJsonObject_stringOf(json,"items[i].md5Hash");
crc32c = CkJsonObject_stringOf(json,"items[i].crc32c");
etag = CkJsonObject_stringOf(json,"items[i].etag");
timeCreated = CkJsonObject_stringOf(json,"items[i].timeCreated");
updated = CkJsonObject_stringOf(json,"items[i].updated");
timeStorageClassUpdated = CkJsonObject_stringOf(json,"items[i].timeStorageClassUpdated");
i = i + 1;
}
CkStringBuilder_Dispose(sbToken);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(json);
}