PHP Extension 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"}'

PHP Extension Example

<?php

// The version number (9_5_0) should match version of the Chilkat extension used, omitting the micro-version number.
// For example, if using Chilkat v9.5.0.48, then include as shown here:
include("chilkat_9_5_0.php");

$rest = new CkRest();

//  URL: https://www.googleapis.com/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT
$bTls = true;
$port = 443;
$bAutoReconnect = true;
$success = $rest->Connect('www.googleapis.com',$port,$bTls,$bAutoReconnect);
if ($success != true) {
    print 'ConnectFailReason: ' . $rest->get_ConnectFailReason() . "\n";
    print $rest->lastErrorText() . "\n";
    exit;
}

//  See the Online Tool for Generating JSON Creation Code
$json = new CkJsonObject();
$json->UpdateString('name','chilkat-test-bucket');

$rest->AddHeader('Authorization','Bearer CLOUD_STORAGE_TOKEN');
$rest->AddHeader('Content-Type','application/json');

$sbRequestBody = new CkStringBuilder();
$json->EmitSb($sbRequestBody);
$sbResponseBody = new CkStringBuilder();
$success = $rest->FullRequestSb('POST','/storage/v1/b?project=MY_CLOUD_STORAGE_PROJECT',$sbRequestBody,$sbResponseBody);
if ($success != true) {
    print $rest->lastErrorText() . "\n";
    exit;
}

$respStatusCode = $rest->get_ResponseStatusCode();
if ($respStatusCode >= 400) {
    print 'Response Status Code = ' . $respStatusCode . "\n";
    print 'Response Header:' . "\n";
    print $rest->responseHeader() . "\n";
    print 'Response Body:' . "\n";
    print $sbResponseBody->getAsString() . "\n";
    exit;
}

$jsonResponse = new CkJsonObject();
$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');

?>

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