Sample code for 30+ languages & platforms
PHP

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 PHP Downloads

PHP
<?php

use Chilkat2\BinData;
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\StringBuilder;

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

$http = new 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

$bdRequestBody = new BinData();
try {
    $bdRequestBody->LoadFile('JSON_FILE_PATH');
} catch (\Chilkat2\Exception $e) {
    echo "Failed to load JSON_FILE_PATH\n";
    exit;
}

// Adds the "Authorization: Bearer OAUTH2_TOKEN" header.
$http->AuthToken = 'OAUTH2_TOKEN';

$resp = new HttpResponse();
try {
    $http->HttpBd('PATCH', 'https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME', $bdRequestBody, 'application/json', $resp);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$sbResponseBody = new StringBuilder();
$resp->GetBodySb($sbResponseBody);

$jResp = new JsonObject();
$jResp->LoadSb($sbResponseBody);
$jResp->EmitCompact = false;

echo "Response Body:\n";
echo $jResp->Emit(), "\n";

$respStatusCode = $resp->StatusCode;
echo 'Response Status Code = ', $respStatusCode, "\n";
if ($respStatusCode >= 400) {
    echo "Response Header:\n";
    echo $resp->Header, "\n";
    echo "Failed.\n";
    exit;
}