C# 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"}'

C# Example

Chilkat.Rest rest = new Chilkat.Rest();
bool success;

//  URL: https://www.googleapis.com/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT
bool bTls = true;
int port = 443;
bool bAutoReconnect = true;
success = rest.Connect("www.googleapis.com",port,bTls,bAutoReconnect);
if (success != true) {
    Debug.WriteLine("ConnectFailReason: " + Convert.ToString(rest.ConnectFailReason));
    Debug.WriteLine(rest.LastErrorText);
    return;
}

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

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

Chilkat.StringBuilder sbRequestBody = new Chilkat.StringBuilder();
json.EmitSb(sbRequestBody);
Chilkat.StringBuilder sbResponseBody = new Chilkat.StringBuilder();
success = rest.FullRequestSb("POST","/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT",sbRequestBody,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 id = jsonResponse.StringOf("id");
string selfLink = jsonResponse.StringOf("selfLink");
string projectNumber = jsonResponse.StringOf("projectNumber");
string name = jsonResponse.StringOf("name");
string timeCreated = jsonResponse.StringOf("timeCreated");
string updated = jsonResponse.StringOf("updated");
string metageneration = jsonResponse.StringOf("metageneration");
string location = jsonResponse.StringOf("location");
string storageClass = jsonResponse.StringOf("storageClass");
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="
}