Unicode C++ Google Cloud Storage: Create Bucket

Back to Index

Creates a new bucket.

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

CURL Command

curl -X POST https://www.googleapis.com/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT \
    --header "Authorization: Bearer CLOUD_STORAGE_TOKEN" \
    --header "Content-Type: application/json" \
    --data '{"name": "chilkat-test-bucket"}'

Unicode C++ Example

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

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

    //  URL: https://www.googleapis.com/storage/v1/b?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;
    }

    //  See the Online Tool for Generating JSON Creation Code
    CkJsonObjectW json;
    json.UpdateString(L"name",L"chilkat-test-bucket");

    rest.AddHeader(L"Authorization",L"Bearer CLOUD_STORAGE_TOKEN");
    rest.AddHeader(L"Content-Type",L"application/json");

    CkStringBuilderW sbRequestBody;
    json.EmitSb(sbRequestBody);
    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestSb(L"POST",L"/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT",sbRequestBody,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

    const wchar_t *kind = jsonResponse.stringOf(L"kind");
    const wchar_t *id = jsonResponse.stringOf(L"id");
    const wchar_t *selfLink = jsonResponse.stringOf(L"selfLink");
    const wchar_t *projectNumber = jsonResponse.stringOf(L"projectNumber");
    const wchar_t *name = jsonResponse.stringOf(L"name");
    const wchar_t *timeCreated = jsonResponse.stringOf(L"timeCreated");
    const wchar_t *updated = jsonResponse.stringOf(L"updated");
    const wchar_t *metageneration = jsonResponse.stringOf(L"metageneration");
    const wchar_t *location = jsonResponse.stringOf(L"location");
    const wchar_t *storageClass = jsonResponse.stringOf(L"storageClass");
    const wchar_t *etag = jsonResponse.stringOf(L"etag");
    }

Sample JSON Response Body

{
  "kind": "storage#bucket",
  "id": "chilkat-test-bucket",
  "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-test-bucket",
  "projectNumber": "5332332985",
  "name": "chilkat-test-bucket",
  "timeCreated": "2018-10-23T11:52:52.464Z",
  "updated": "2018-10-23T11:52:52.464Z",
  "metageneration": "1",
  "location": "US",
  "storageClass": "STANDARD",
  "etag": "CAE="
}