Delphi DLL Google Cloud Storage: List Objects in a Bucket

Back to Index

Retrieves a list of objects in the Google Cloud Storage bucket. The name of the bucket is specified in the URL's path. This example retrieves a listing of the objects in the "chilkat-test" bucket.

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

CURL Command

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

Delphi DLL Example

var
rest: HCkRest;
success: Boolean;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
sbResponseBody: HCkStringBuilder;
respStatusCode: Integer;
jsonResponse: HCkJsonObject;
i: Integer;
count_i: Integer;
kind: PWideChar;
id: PWideChar;
selfLink: PWideChar;
name: PWideChar;
bucket: PWideChar;
generation: PWideChar;
metageneration: PWideChar;
contentType: PWideChar;
timeCreated: PWideChar;
updated: PWideChar;
storageClass: PWideChar;
timeStorageClassUpdated: PWideChar;
size: PWideChar;
md5Hash: PWideChar;
mediaLink: PWideChar;
crc32c: PWideChar;
etag: PWideChar;

begin
rest := CkRest_Create();

//  URL: https://www.googleapis.com/storage/v1/b/chilkat-bucket/o?project=MY_CLOUD_STORAGE_PROJECT
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'www.googleapis.com',port,bTls,bAutoReconnect);
if (success <> True) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(CkRest_getConnectFailReason(rest)));
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

CkRest_AddHeader(rest,'Authorization','Bearer CLOUD_STORAGE_TOKEN');

sbResponseBody := CkStringBuilder_Create();
success := CkRest_FullRequestNoBodySb(rest,'GET','/storage/v1/b/chilkat-bucket/o?project=MY_CLOUD_STORAGE_PROJECT',sbResponseBody);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;
respStatusCode := CkRest_getResponseStatusCode(rest);
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkRest__responseHeader(rest));
    Memo1.Lines.Add('Response Body:');
    Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponseBody));
    Exit;
  end;

jsonResponse := CkJsonObject_Create();
CkJsonObject_LoadSb(jsonResponse,sbResponseBody);

//  See the Online Tool for Generating JSON Parse Code

kind := CkJsonObject__stringOf(jsonResponse,'kind');
i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResponse,'items');
while i < count_i do
  begin
CkJsonObject_putI(jsonResponse,i);
    kind := CkJsonObject__stringOf(jsonResponse,'items[i].kind');
    id := CkJsonObject__stringOf(jsonResponse,'items[i].id');
    selfLink := CkJsonObject__stringOf(jsonResponse,'items[i].selfLink');
    name := CkJsonObject__stringOf(jsonResponse,'items[i].name');
    bucket := CkJsonObject__stringOf(jsonResponse,'items[i].bucket');
    generation := CkJsonObject__stringOf(jsonResponse,'items[i].generation');
    metageneration := CkJsonObject__stringOf(jsonResponse,'items[i].metageneration');
    contentType := CkJsonObject__stringOf(jsonResponse,'items[i].contentType');
    timeCreated := CkJsonObject__stringOf(jsonResponse,'items[i].timeCreated');
    updated := CkJsonObject__stringOf(jsonResponse,'items[i].updated');
    storageClass := CkJsonObject__stringOf(jsonResponse,'items[i].storageClass');
    timeStorageClassUpdated := CkJsonObject__stringOf(jsonResponse,'items[i].timeStorageClassUpdated');
    size := CkJsonObject__stringOf(jsonResponse,'items[i].size');
    md5Hash := CkJsonObject__stringOf(jsonResponse,'items[i].md5Hash');
    mediaLink := CkJsonObject__stringOf(jsonResponse,'items[i].mediaLink');
    crc32c := CkJsonObject__stringOf(jsonResponse,'items[i].crc32c');
    etag := CkJsonObject__stringOf(jsonResponse,'items[i].etag');
    i := i + 1;
  end;

CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jsonResponse);

Sample JSON Response Body

{
  "kind": "storage#objects",
  "items": [
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/hedgehogs.jpg/1540253106638630",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/hedgehogs.jpg",
      "name": "hedgehogs.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106638630",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.638Z",
      "updated": "2018-10-23T00:05:06.638Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.638Z",
      "size": "48573",
      "md5Hash": "TE1U6fQlD6MOVGaQbwGinQ==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/hedgehogs.jpg?generation=1540253106638630&alt=media",
      "crc32c": "1B2d6g==",
      "etag": "CKaepPqhm94CEAE="
    },
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/penguins.jpg/1540253106941673",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/penguins.jpg",
      "name": "penguins.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106941673",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.941Z",
      "updated": "2018-10-23T00:05:06.941Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.941Z",
      "size": "777835",
      "md5Hash": "nTd7EM53jEk4s8fixjoimg==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/penguins.jpg?generation=1540253106941673&alt=media",
      "crc32c": "ixxYVw==",
      "etag": "COndtvqhm94CEAE="
    },
    {
      "kind": "storage#object",
      "id": "chilkat-bucket/starfish.jpg/1540253106637454",
      "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/starfish.jpg",
      "name": "starfish.jpg",
      "bucket": "chilkat-bucket",
      "generation": "1540253106637454",
      "metageneration": "1",
      "contentType": "image/jpeg",
      "timeCreated": "2018-10-23T00:05:06.636Z",
      "updated": "2018-10-23T00:05:06.636Z",
      "storageClass": "MULTI_REGIONAL",
      "timeStorageClassUpdated": "2018-10-23T00:05:06.636Z",
      "size": "6229",
      "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
      "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-bucket/o/starfish.jpg?generation=1540253106637454&alt=media",
      "crc32c": "9RjgwQ==",
      "etag": "CI6VpPqhm94CEAE="
    }
  ]
}