Delphi ActiveX 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"

Delphi ActiveX Example

var
rest: TChilkatRest;
success: Integer;
bTls: Integer;
port: Integer;
bAutoReconnect: Integer;
sbResponseBody: TChilkatStringBuilder;
respStatusCode: Integer;
jsonResponse: TChilkatJsonObject;
kind: WideString;
totalBytesRewritten: WideString;
objectSize: WideString;
done: Integer;
resourceKind: WideString;
resourceId: WideString;
resourceSelfLink: WideString;
resourceName: WideString;
resourceBucket: WideString;
resourceGeneration: WideString;
resourceMetageneration: WideString;
resourceContentType: WideString;
resourceTimeCreated: WideString;
resourceUpdated: WideString;
resourceStorageClass: WideString;
resourceTimeStorageClassUpdated: WideString;
resourceSize: WideString;
resourceMd5Hash: WideString;
resourceMediaLink: WideString;
resourceCrc32c: WideString;
resourceEtag: WideString;

begin
rest := TChilkatRest.Create(Self);

//  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) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(rest.ConnectFailReason));
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

rest.AddHeader('Authorization','Bearer CLOUD_STORAGE_TOKEN');

sbResponseBody := TChilkatStringBuilder.Create(Self);
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.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;
respStatusCode := rest.ResponseStatusCode;
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(rest.ResponseHeader);
    Memo1.Lines.Add('Response Body:');
    Memo1.Lines.Add(sbResponseBody.GetAsString());
    Exit;
  end;

jsonResponse := TChilkatJsonObject.Create(Self);
jsonResponse.LoadSb(sbResponseBody.ControlInterface);

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