Delphi ActiveX Google Cloud Storage: List Buckets

Back to Index

Retrieves a list of buckets for a given project.

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

CURL Command

curl -X GET https://www.googleapis.com/storage/v1/b \
    --header "Authorization: Bearer CLOUD_STORAGE_TOKEN" \
    --data "project=MY_CLOUD_STORAGE_PROJECT"

Delphi ActiveX Example

var
rest: TChilkatRest;
success: Integer;
bTls: Integer;
port: Integer;
bAutoReconnect: Integer;
sbResponseBody: TChilkatStringBuilder;
respStatusCode: Integer;
jsonResponse: TChilkatJsonObject;
i: Integer;
count_i: Integer;
kind: WideString;
id: WideString;
selfLink: WideString;
projectNumber: WideString;
name: WideString;
timeCreated: WideString;
updated: WideString;
metageneration: WideString;
location: WideString;
storageClass: WideString;
etag: WideString;

begin
rest := TChilkatRest.Create(Self);

//  URL: https://www.googleapis.com/storage/v1/b
bTls := 1;
port := 443;
bAutoReconnect := 1;
success := rest.Connect('www.googleapis.com',port,bTls,bAutoReconnect);
if (success <> 1) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(rest.ConnectFailReason));
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

rest.AddQueryParam('project','MY_CLOUD_STORAGE_PROJECT');

rest.AddHeader('Authorization','Bearer CLOUD_STORAGE_TOKEN');

sbResponseBody := TChilkatStringBuilder.Create(Self);
success := rest.FullRequestNoBodySb('GET','/storage/v1/b',sbResponseBody.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;
respStatusCode := rest.ResponseStatusCode;
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(rest.ResponseHeader);
    Memo1.Lines.Add('Response Body:');
    Memo1.Lines.Add(sbResponseBody.GetAsString());
    Exit;
  end;

jsonResponse := TChilkatJsonObject.Create(Self);
jsonResponse.LoadSb(sbResponseBody.ControlInterface);

//  See the Online Tool for Generating JSON Parse Code

kind := jsonResponse.StringOf('kind');
i := 0;
count_i := jsonResponse.SizeOfArray('items');
while i < count_i do
  begin
    jsonResponse.I := i;
    kind := jsonResponse.StringOf('items[i].kind');
    id := jsonResponse.StringOf('items[i].id');
    selfLink := jsonResponse.StringOf('items[i].selfLink');
    projectNumber := jsonResponse.StringOf('items[i].projectNumber');
    name := jsonResponse.StringOf('items[i].name');
    timeCreated := jsonResponse.StringOf('items[i].timeCreated');
    updated := jsonResponse.StringOf('items[i].updated');
    metageneration := jsonResponse.StringOf('items[i].metageneration');
    location := jsonResponse.StringOf('items[i].location');
    storageClass := jsonResponse.StringOf('items[i].storageClass');
    etag := jsonResponse.StringOf('items[i].etag');
    i := i + 1;
  end;

Sample JSON Response Body

{
  "kind": "storage#buckets",
  "items": [
    {
      "kind": "storage#bucket",
      "id": "chilkat-bucket",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket",
      "projectNumber": "5332332985",
      "name": "chilkat-bucket",
      "timeCreated": "2018-10-23T00:04:44.507Z",
      "updated": "2018-10-23T00:04:44.507Z",
      "metageneration": "1",
      "location": "US",
      "storageClass": "MULTI_REGIONAL",
      "etag": "CAE="
    },
    {
      "kind": "storage#bucket",
      "id": "chilkat-images",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-images",
      "projectNumber": "5332332985",
      "name": "chilkat-images",
      "timeCreated": "2018-10-23T11:24:43.000Z",
      "updated": "2018-10-23T11:24:43.000Z",
      "metageneration": "1",
      "location": "US",
      "storageClass": "MULTI_REGIONAL",
      "etag": "CAE="
    }
  ]
}