VB.NET 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"

VB.NET Example

Dim rest As New Chilkat.Rest
Dim success As Boolean

'  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
Dim bTls As Boolean = True
Dim port As Integer = 443
Dim bAutoReconnect As Boolean = True
success = rest.Connect("www.googleapis.com",port,bTls,bAutoReconnect)
If (success <> True) Then
    Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
    Debug.WriteLine(rest.LastErrorText)
    Exit Sub
End If


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

Dim sbResponseBody As New Chilkat.StringBuilder
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.WriteLine(rest.LastErrorText)
    Exit Sub
End If

Dim respStatusCode As Integer = rest.ResponseStatusCode
If (respStatusCode >= 400) Then
    Debug.WriteLine("Response Status Code = " & respStatusCode)
    Debug.WriteLine("Response Header:")
    Debug.WriteLine(rest.ResponseHeader)
    Debug.WriteLine("Response Body:")
    Debug.WriteLine(sbResponseBody.GetAsString())
    Exit Sub
End If


Dim jsonResponse As New Chilkat.JsonObject
jsonResponse.LoadSb(sbResponseBody)

'  See the Online Tool for Generating JSON Parse Code

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