Sample code for 30+ languages & platforms
C++

Google Cloud Storage: Update Object Metadata

See more Google Cloud Storage Examples

Demonstrates how to update (edit) the metadata associated with an object in a Google Cloud Storage bucket.

Chilkat C++ Downloads

C++
#include <CkHttp.h>
#include <CkBinData.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>
#include <CkJsonObject.h>

void ChilkatSample(void)
    {
    bool success = false;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttp http;

    // Implements the following CURL command:

    // curl -X PATCH --data-binary @JSON_FILE_NAME \
    //   -H "Authorization: Bearer OAUTH2_TOKEN" \
    //   -H "Content-Type: application/json" \
    //   "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME"

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    CkBinData bdRequestBody;
    success = bdRequestBody.LoadFile("JSON_FILE_PATH");
    if (success != true) {
        std::cout << "Failed to load JSON_FILE_PATH" << "\r\n";
        return;
    }

    // Adds the "Authorization: Bearer OAUTH2_TOKEN" header.
    http.put_AuthToken("OAUTH2_TOKEN");

    CkHttpResponse resp;
    success = http.HttpBd("PATCH","https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME",bdRequestBody,"application/json",resp);
    if (success == false) {
        std::cout << http.lastErrorText() << "\r\n";
        return;
    }

    CkStringBuilder sbResponseBody;
    resp.GetBodySb(sbResponseBody);

    CkJsonObject jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    std::cout << "Response Body:" << "\r\n";
    std::cout << jResp.emit() << "\r\n";

    int respStatusCode = resp.get_StatusCode();
    std::cout << "Response Status Code = " << respStatusCode << "\r\n";
    if (respStatusCode >= 400) {
        std::cout << "Response Header:" << "\r\n";
        std::cout << resp.header() << "\r\n";
        std::cout << "Failed." << "\r\n";
        return;
    }
    }