Xojo Plugin 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"}'

Xojo Plugin Example

Dim rest As New Chilkat.Rest
Dim success As Boolean

//  URL: https://www.googleapis.com/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT
Dim bTls As Boolean
bTls = True
Dim port As Int32
port = 443
Dim bAutoReconnect As Boolean
bAutoReconnect = True
success = rest.Connect("www.googleapis.com",port,bTls,bAutoReconnect)
If (success <> True) Then
    System.DebugLog("ConnectFailReason: " + Str(rest.ConnectFailReason))
    System.DebugLog(rest.LastErrorText)
    Return
End If

//  See the Online Tool for Generating JSON Creation Code
Dim json As New Chilkat.JsonObject
success = json.UpdateString("name","chilkat-test-bucket")

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

Dim sbRequestBody As New Chilkat.StringBuilder
success = json.EmitSb(sbRequestBody)
Dim sbResponseBody As New Chilkat.StringBuilder
success = rest.FullRequestSb("POST","/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT",sbRequestBody,sbResponseBody)
If (success <> True) Then
    System.DebugLog(rest.LastErrorText)
    Return
End If

Dim respStatusCode As Int32
respStatusCode = rest.ResponseStatusCode
If (respStatusCode >= 400) Then
    System.DebugLog("Response Status Code = " + Str(respStatusCode))
    System.DebugLog("Response Header:")
    System.DebugLog(rest.ResponseHeader)
    System.DebugLog("Response Body:")
    System.DebugLog(sbResponseBody.GetAsString())
    Return
End If

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

//  See the Online Tool for Generating JSON Parse Code

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

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