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 <C_CkRestW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkStringBuilderW sbResponseBody;
    int respStatusCode;
    HCkJsonObjectW jsonResponse;
    const wchar_t *kind;
    const wchar_t *totalBytesRewritten;
    const wchar_t *objectSize;
    BOOL done;
    const wchar_t *resourceKind;
    const wchar_t *resourceId;
    const wchar_t *resourceSelfLink;
    const wchar_t *resourceName;
    const wchar_t *resourceBucket;
    const wchar_t *resourceGeneration;
    const wchar_t *resourceMetageneration;
    const wchar_t *resourceContentType;
    const wchar_t *resourceTimeCreated;
    const wchar_t *resourceUpdated;
    const wchar_t *resourceStorageClass;
    const wchar_t *resourceTimeStorageClassUpdated;
    const wchar_t *resourceSize;
    const wchar_t *resourceMd5Hash;
    const wchar_t *resourceMediaLink;
    const wchar_t *resourceCrc32c;
    const wchar_t *resourceEtag;

    rest = CkRestW_Create();

    //  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 = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"www.googleapis.com",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        wprintf(L"ConnectFailReason: %d\n",CkRestW_getConnectFailReason(rest));
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

    CkRestW_AddHeader(rest,L"Authorization",L"Bearer CLOUD_STORAGE_TOKEN");

    sbResponseBody = CkStringBuilderW_Create();
    success = CkRestW_FullRequestNoBodySb(rest,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",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    respStatusCode = CkRestW_getResponseStatusCode(rest);
    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkRestW_responseHeader(rest));
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponseBody));
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    jsonResponse = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jsonResponse,sbResponseBody);

    //  See the Online Tool for Generating JSON Parse Code

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


    CkRestW_Dispose(rest);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jsonResponse);

    }

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="
  }
}