VBScript Google Cloud Storage: Create Bucket

Back to Index

Creates a new bucket.

Documentation: https://cloud.google.com/storage/docs/json_api/v1/buckets/insert

CURL Command

curl -X POST https://www.googleapis.com/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT \
    --header "Authorization: Bearer CLOUD_STORAGE_TOKEN" \
    --header "Content-Type: application/json" \
    --data '{"name": "chilkat-test-bucket"}'

VBScript Example

Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)

set rest = CreateObject("Chilkat_9_5_0.Rest")

'  URL: https://www.googleapis.com/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT
bTls = 1
port = 443
bAutoReconnect = 1
success = rest.Connect("www.googleapis.com",port,bTls,bAutoReconnect)
If (success <> 1) Then
    outFile.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
    outFile.WriteLine(rest.LastErrorText)
    WScript.Quit
End If

'  See the Online Tool for Generating JSON Creation Code
set json = CreateObject("Chilkat_9_5_0.JsonObject")
success = json.UpdateString("name","chilkat-test-bucket")

success = rest.AddHeader("Authorization","Bearer CLOUD_STORAGE_TOKEN")
success = rest.AddHeader("Content-Type","application/json")

set sbRequestBody = CreateObject("Chilkat_9_5_0.StringBuilder")
success = json.EmitSb(sbRequestBody)
set sbResponseBody = CreateObject("Chilkat_9_5_0.StringBuilder")
success = rest.FullRequestSb("POST","/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT",sbRequestBody,sbResponseBody)
If (success <> 1) Then
    outFile.WriteLine(rest.LastErrorText)
    WScript.Quit
End If

respStatusCode = rest.ResponseStatusCode
If (respStatusCode >= 400) Then
    outFile.WriteLine("Response Status Code = " & respStatusCode)
    outFile.WriteLine("Response Header:")
    outFile.WriteLine(rest.ResponseHeader)
    outFile.WriteLine("Response Body:")
    outFile.WriteLine(sbResponseBody.GetAsString())
    WScript.Quit
End If

set jsonResponse = CreateObject("Chilkat_9_5_0.JsonObject")
success = jsonResponse.LoadSb(sbResponseBody)

'  See the Online Tool for Generating JSON Parse Code

kind = jsonResponse.StringOf("kind")
id = jsonResponse.StringOf("id")
selfLink = jsonResponse.StringOf("selfLink")
projectNumber = jsonResponse.StringOf("projectNumber")
name = jsonResponse.StringOf("name")
timeCreated = jsonResponse.StringOf("timeCreated")
updated = jsonResponse.StringOf("updated")
metageneration = jsonResponse.StringOf("metageneration")
location = jsonResponse.StringOf("location")
storageClass = jsonResponse.StringOf("storageClass")
etag = jsonResponse.StringOf("etag")

outFile.Close

Sample JSON Response Body

{
  "kind": "storage#bucket",
  "id": "chilkat-test-bucket",
  "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-test-bucket",
  "projectNumber": "5332332985",
  "name": "chilkat-test-bucket",
  "timeCreated": "2018-10-23T11:52:52.464Z",
  "updated": "2018-10-23T11:52:52.464Z",
  "metageneration": "1",
  "location": "US",
  "storageClass": "STANDARD",
  "etag": "CAE="
}