Perl
Perl
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 Perl Downloads
use strict;
use warnings;
use Chilkat2;
# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
my $http = Chilkat2::Http->new;
# 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
my $bdRequestBody = Chilkat2::BinData->new;
my $success = $bdRequestBody->LoadFile('JSON_FILE_PATH');
if (!$success) {
print "Failed to load JSON_FILE_PATH\n";
exit;
}
# Adds the "Authorization: Bearer OAUTH2_TOKEN" header.
$http->AuthToken('OAUTH2_TOKEN');
my $resp = Chilkat2::HttpResponse->new;
$success = $http->HttpBd('PATCH', 'https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME', $bdRequestBody, 'application/json', $resp);
if (!$success) {
print $http->LastErrorText, "\n";
exit;
}
my $sbResponseBody = Chilkat2::StringBuilder->new;
$resp->GetBodySb($sbResponseBody);
my $jResp = Chilkat2::JsonObject->new;
$jResp->LoadSb($sbResponseBody);
$jResp->EmitCompact(0);
print "Response Body:\n";
print $jResp->Emit(), "\n";
my $respStatusCode = $resp->StatusCode;
print 'Response Status Code = ', $respStatusCode, "\n";
if ($respStatusCode >= 400) {
print "Response Header:\n";
print $resp->Header, "\n";
print "Failed.\n";
exit;
}