VBScript 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"

VBScript Example

Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)

set rest = CreateObject("Chilkat_9_5_0.Rest")

'  URL: https://www.googleapis.com/storage/v1/b/chilkat-bucket/o?project=MY_CLOUD_STORAGE_PROJECT
bTls = 1
port = 443
bAutoReconnect = 1
success = rest.Connect("www.googleapis.com",port,bTls,bAutoReconnect)
If (success <> 1) Then
    outFile.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
    outFile.WriteLine(rest.LastErrorText)
    WScript.Quit
End If

success = rest.AddHeader("Authorization","Bearer CLOUD_STORAGE_TOKEN")

set sbResponseBody = CreateObject("Chilkat_9_5_0.StringBuilder")
success = rest.FullRequestNoBodySb("GET","/storage/v1/b/chilkat-bucket/o?project=MY_CLOUD_STORAGE_PROJECT",sbResponseBody)
If (success <> 1) Then
    outFile.WriteLine(rest.LastErrorText)
    WScript.Quit
End If

respStatusCode = rest.ResponseStatusCode
If (respStatusCode >= 400) Then
    outFile.WriteLine("Response Status Code = " & respStatusCode)
    outFile.WriteLine("Response Header:")
    outFile.WriteLine(rest.ResponseHeader)
    outFile.WriteLine("Response Body:")
    outFile.WriteLine(sbResponseBody.GetAsString())
    WScript.Quit
End If

set jsonResponse = CreateObject("Chilkat_9_5_0.JsonObject")
success = jsonResponse.LoadSb(sbResponseBody)

'  See the Online Tool for Generating JSON Parse Code

kind = jsonResponse.StringOf("kind")
i = 0
count_i = jsonResponse.SizeOfArray("items")
Do While i < count_i
    jsonResponse.I = i
    kind = jsonResponse.StringOf("items[i].kind")
    id = jsonResponse.StringOf("items[i].id")
    selfLink = jsonResponse.StringOf("items[i].selfLink")
    name = jsonResponse.StringOf("items[i].name")
    bucket = jsonResponse.StringOf("items[i].bucket")
    generation = jsonResponse.StringOf("items[i].generation")
    metageneration = jsonResponse.StringOf("items[i].metageneration")
    contentType = jsonResponse.StringOf("items[i].contentType")
    timeCreated = jsonResponse.StringOf("items[i].timeCreated")
    updated = jsonResponse.StringOf("items[i].updated")
    storageClass = jsonResponse.StringOf("items[i].storageClass")
    timeStorageClassUpdated = jsonResponse.StringOf("items[i].timeStorageClassUpdated")
    size = jsonResponse.StringOf("items[i].size")
    md5Hash = jsonResponse.StringOf("items[i].md5Hash")
    mediaLink = jsonResponse.StringOf("items[i].mediaLink")
    crc32c = jsonResponse.StringOf("items[i].crc32c")
    etag = jsonResponse.StringOf("items[i].etag")
    i = i + 1
Loop

outFile.Close

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