C# UWP/WinRT 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"

C# UWP/WinRT Example

Chilkat.Rest rest = new Chilkat.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 = await rest.ConnectAsync("www.googleapis.com",port,bTls,bAutoReconnect);
if (success != true) {
    Debug.WriteLine("ConnectFailReason: " + Convert.ToString(rest.ConnectFailReason));
    Debug.WriteLine(rest.LastErrorText);
    return;
}

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

Chilkat.StringBuilder sbResponseBody = new Chilkat.StringBuilder();
success = await rest.FullRequestNoBodySbAsync("POST","/storage/v1/b/chilkat-bucket/o/starfish.jpg/rewriteTo/b/chilkat-images/o/starfish.jpg?project=MY_CLOUD_STORAGE_PROJECT",sbResponseBody);
if (success != true) {
    Debug.WriteLine(rest.LastErrorText);
    return;
}

int respStatusCode = rest.ResponseStatusCode;
if (respStatusCode >= 400) {
    Debug.WriteLine("Response Status Code = " + Convert.ToString(respStatusCode));
    Debug.WriteLine("Response Header:");
    Debug.WriteLine(rest.ResponseHeader);
    Debug.WriteLine("Response Body:");
    Debug.WriteLine(sbResponseBody.GetAsString());
    return;
}

Chilkat.JsonObject jsonResponse = new Chilkat.JsonObject();
jsonResponse.LoadSb(sbResponseBody);

//  See the Online Tool for Generating JSON Parse Code

string kind = jsonResponse.StringOf("kind");
string totalBytesRewritten = jsonResponse.StringOf("totalBytesRewritten");
string objectSize = jsonResponse.StringOf("objectSize");
bool done = jsonResponse.BoolOf("done");
string resourceKind = jsonResponse.StringOf("resource.kind");
string resourceId = jsonResponse.StringOf("resource.id");
string resourceSelfLink = jsonResponse.StringOf("resource.selfLink");
string resourceName = jsonResponse.StringOf("resource.name");
string resourceBucket = jsonResponse.StringOf("resource.bucket");
string resourceGeneration = jsonResponse.StringOf("resource.generation");
string resourceMetageneration = jsonResponse.StringOf("resource.metageneration");
string resourceContentType = jsonResponse.StringOf("resource.contentType");
string resourceTimeCreated = jsonResponse.StringOf("resource.timeCreated");
string resourceUpdated = jsonResponse.StringOf("resource.updated");
string resourceStorageClass = jsonResponse.StringOf("resource.storageClass");
string resourceTimeStorageClassUpdated = jsonResponse.StringOf("resource.timeStorageClassUpdated");
string resourceSize = jsonResponse.StringOf("resource.size");
string resourceMd5Hash = jsonResponse.StringOf("resource.md5Hash");
string resourceMediaLink = jsonResponse.StringOf("resource.mediaLink");
string resourceCrc32c = jsonResponse.StringOf("resource.crc32c");
string 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="
  }
}