Excel 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"

Excel Example

Dim rest As Chilkat.Rest
Set rest = Chilkat.NewRest

'  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 = rest.Connect("www.googleapis.com",port,bTls,bAutoReconnect)
If (success <> True) Then
    Debug.Print "ConnectFailReason: "; rest.ConnectFailReason
    Debug.Print rest.LastErrorText
    Exit Sub
End If

success = rest.AddHeader("Authorization","Bearer CLOUD_STORAGE_TOKEN")

Dim sbResponseBody As Chilkat.StringBuilder
Set sbResponseBody = Chilkat.NewStringBuilder
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 <> True) Then
    Debug.Print rest.LastErrorText
    Exit Sub
End If


respStatusCode = rest.ResponseStatusCode
If (respStatusCode >= 400) Then
    Debug.Print "Response Status Code = "; respStatusCode
    Debug.Print "Response Header:"
    Debug.Print rest.ResponseHeader
    Debug.Print "Response Body:"
    Debug.Print sbResponseBody.GetAsString()
    Exit Sub
End If

Dim jsonResponse As Chilkat.JsonObject
Set jsonResponse = Chilkat.NewJsonObject
success = 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="
  }
}