Perl Google Cloud Storage: Copy File (Object) to another Bucket

Back to Index

Copies a fie (also known as an object) to another Google Cloud Storage bucket. This example copies the file "starfish.jpg" from the "chilkat-test" bucket to the "chilkat-images" bucket. In Google Cloud Storage, moving a file to a new bucket is a 2-step operation: First copy the file, then delete the original.

Documentation: https://cloud.google.com/storage/docs/renaming-copying-moving-objects

CURL Command

curl -X POST https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/starfish.jpg/rewriteTo/b/chilkat-images/o/starfish.jpg?project=MY_CLOUD_STORAGE_PROJECT \
    --header "Authorization: Bearer CLOUD_STORAGE_TOKEN"

Perl Example

use chilkat();

$rest = chilkat::CkRest->new();

#  URL: https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/starfish.jpg/rewriteTo/b/chilkat-images/o/starfish.jpg?project=MY_CLOUD_STORAGE_PROJECT
$bTls = 1;
$port = 443;
$bAutoReconnect = 1;
$success = $rest->Connect("www.googleapis.com",$port,$bTls,$bAutoReconnect);
if ($success != 1) {
    print "ConnectFailReason: " . $rest->get_ConnectFailReason() . "\r\n";
    print $rest->lastErrorText() . "\r\n";
    exit;
}

$rest->AddHeader("Authorization","Bearer CLOUD_STORAGE_TOKEN");

$sbResponseBody = chilkat::CkStringBuilder->new();
$success = $rest->FullRequestNoBodySb("POST","/storage/v1/b/chilkat-bucket/o/starfish.jpg/rewriteTo/b/chilkat-images/o/starfish.jpg?project=MY_CLOUD_STORAGE_PROJECT",$sbResponseBody);
if ($success != 1) {
    print $rest->lastErrorText() . "\r\n";
    exit;
}

$respStatusCode = $rest->get_ResponseStatusCode();
if ($respStatusCode >= 400) {
    print "Response Status Code = " . $respStatusCode . "\r\n";
    print "Response Header:" . "\r\n";
    print $rest->responseHeader() . "\r\n";
    print "Response Body:" . "\r\n";
    print $sbResponseBody->getAsString() . "\r\n";
    exit;
}

$jsonResponse = chilkat::CkJsonObject->new();
$jsonResponse->LoadSb($sbResponseBody);

#  See the Online Tool for Generating JSON Parse Code

$kind = $jsonResponse->stringOf("kind");
$totalBytesRewritten = $jsonResponse->stringOf("totalBytesRewritten");
$objectSize = $jsonResponse->stringOf("objectSize");
$done = $jsonResponse->BoolOf("done");
$resourceKind = $jsonResponse->stringOf("resource.kind");
$resourceId = $jsonResponse->stringOf("resource.id");
$resourceSelfLink = $jsonResponse->stringOf("resource.selfLink");
$resourceName = $jsonResponse->stringOf("resource.name");
$resourceBucket = $jsonResponse->stringOf("resource.bucket");
$resourceGeneration = $jsonResponse->stringOf("resource.generation");
$resourceMetageneration = $jsonResponse->stringOf("resource.metageneration");
$resourceContentType = $jsonResponse->stringOf("resource.contentType");
$resourceTimeCreated = $jsonResponse->stringOf("resource.timeCreated");
$resourceUpdated = $jsonResponse->stringOf("resource.updated");
$resourceStorageClass = $jsonResponse->stringOf("resource.storageClass");
$resourceTimeStorageClassUpdated = $jsonResponse->stringOf("resource.timeStorageClassUpdated");
$resourceSize = $jsonResponse->stringOf("resource.size");
$resourceMd5Hash = $jsonResponse->stringOf("resource.md5Hash");
$resourceMediaLink = $jsonResponse->stringOf("resource.mediaLink");
$resourceCrc32c = $jsonResponse->stringOf("resource.crc32c");
$resourceEtag = $jsonResponse->stringOf("resource.etag");

Sample JSON Response Body

{
  "kind": "storage#rewriteResponse",
  "totalBytesRewritten": "6229",
  "objectSize": "6229",
  "done": true,
  "resource": {
    "kind": "storage#object",
    "id": "chilkat-images/starfish.jpg/1540298057547474",
    "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-images/o/starfish.jpg",
    "name": "starfish.jpg",
    "bucket": "chilkat-images",
    "generation": "1540298057547474",
    "metageneration": "1",
    "contentType": "image/jpeg",
    "timeCreated": "2018-10-23T12:34:17.547Z",
    "updated": "2018-10-23T12:34:17.547Z",
    "storageClass": "MULTI_REGIONAL",
    "timeStorageClassUpdated": "2018-10-23T12:34:17.547Z",
    "size": "6229",
    "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
    "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-images/o/starfish.jpg?generation=1540298057547474&alt=media",
    "crc32c": "9RjgwQ==",
    "etag": "CNL9xbTJnN4CEAE="
  }
}