Unicode C++ 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"

Unicode C++ Example

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

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

    //  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
    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;
    }

    rest.AddHeader(L"Authorization",L"Bearer CLOUD_STORAGE_TOKEN");

    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestNoBodySb(L"POST",L"/storage/v1/b/chilkat-bucket/o/starfish.jpg/rewriteTo/b/chilkat-images/o/starfish.jpg?project=MY_CLOUD_STORAGE_PROJECT",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 *totalBytesRewritten = jsonResponse.stringOf(L"totalBytesRewritten");
    const wchar_t *objectSize = jsonResponse.stringOf(L"objectSize");
    bool done = jsonResponse.BoolOf(L"done");
    const wchar_t *resourceKind = jsonResponse.stringOf(L"resource.kind");
    const wchar_t *resourceId = jsonResponse.stringOf(L"resource.id");
    const wchar_t *resourceSelfLink = jsonResponse.stringOf(L"resource.selfLink");
    const wchar_t *resourceName = jsonResponse.stringOf(L"resource.name");
    const wchar_t *resourceBucket = jsonResponse.stringOf(L"resource.bucket");
    const wchar_t *resourceGeneration = jsonResponse.stringOf(L"resource.generation");
    const wchar_t *resourceMetageneration = jsonResponse.stringOf(L"resource.metageneration");
    const wchar_t *resourceContentType = jsonResponse.stringOf(L"resource.contentType");
    const wchar_t *resourceTimeCreated = jsonResponse.stringOf(L"resource.timeCreated");
    const wchar_t *resourceUpdated = jsonResponse.stringOf(L"resource.updated");
    const wchar_t *resourceStorageClass = jsonResponse.stringOf(L"resource.storageClass");
    const wchar_t *resourceTimeStorageClassUpdated = jsonResponse.stringOf(L"resource.timeStorageClassUpdated");
    const wchar_t *resourceSize = jsonResponse.stringOf(L"resource.size");
    const wchar_t *resourceMd5Hash = jsonResponse.stringOf(L"resource.md5Hash");
    const wchar_t *resourceMediaLink = jsonResponse.stringOf(L"resource.mediaLink");
    const wchar_t *resourceCrc32c = jsonResponse.stringOf(L"resource.crc32c");
    const wchar_t *resourceEtag = jsonResponse.stringOf(L"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="
  }
}