Sample code for 30+ languages & platforms
Swift

Google Cloud Storage List Bucket Objects

See more Google Cloud Storage Examples

List the objects in a Google Cloud Storage bucket.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example uses a previously obtained access token having permission for the 
    // scope "https://www.googleapis.com/auth/cloud-platform"

    // In this example, Get Google Cloud Storage OAuth2 Access Token, 
    // the service account access token was saved to a text file.  This example fetches the access token from the file..
    let sbToken = CkoStringBuilder()!
    sbToken.loadFile(path: "qa_data/tokens/googleCloudStorageAccessToken.txt", charset: "utf-8")

    let http = CkoHttp()!
    http.authToken = sbToken.getAsString()

    // For more info see Cloud Storage Documentation - Objects: list 
    // 
    success = http.setUrlVar(name: "bucket", value: "chilkat-ocean")
    success = http.setUrlVar(name: "PROJECT_NAME", value: "ChilkatCloud")

    let resp = CkoHttpResponse()!
    success = http.httpNoBody(verb: "GET", url: "https://www.googleapis.com/storage/v1/b/{$bucket}/o?project={$ChilkatCloud}", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    var responseCode: Int = resp.statusCode.intValue
    if responseCode == 401 {
        print("\(resp.bodyStr!)")
        print("If invalid credentials, then it is likely the access token expired.")
        print("Your app should automatically fetch a new access token and re-try.")
        return
    }

    print("Response code: \(responseCode)")
    print("Response body")

    let json = CkoJsonObject()!
    success = json.load(json: resp.bodyStr)

    json.emitCompact = false

    print("\(json.emit()!)")

    // A response code = 200 indicates success, and the response body contains JSON such as this:

    // {
    //   "kind": "storage#objects",
    //   "items": [
    //     {
    //       "kind": "storage#object",
    //       "id": "chilkat-ocean/file with space char.jpg/1555776690752366",
    //       "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg",
    //       "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg?generation=1555776690752366&alt=media",
    //       "name": "file with space char.jpg",
    //       "bucket": "chilkat-ocean",
    //       "generation": "1555776690752366",
    //       "metageneration": "1",
    //       "contentType": "image/jpeg",
    //       "storageClass": "MULTI_REGIONAL",
    //       "size": "6229",
    //       "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
    //       "crc32c": "9RjgwQ==",
    //       "etag": "CO6WguiH3+ECEAE=",
    //       "timeCreated": "2019-04-20T16:11:30.752Z",
    //       "updated": "2019-04-20T16:11:30.752Z",
    //       "timeStorageClassUpdated": "2019-04-20T16:11:30.752Z"
    //     },
    //     {
    //       "kind": "storage#object",
    // 
    //       "id": "chilkat-ocean/penguins.jpg/1555775749593296",
    //       "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/penguins.jpg",
    //       "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/penguins.jpg?generation=1555775749593296&alt=media",
    //       "name": "penguins.jpg",
    //       "bucket": "chilkat-ocean",
    //       "generation": "1555775749593296",
    //       "metageneration": "1",
    //       "contentType": "image/jpeg",
    //       "storageClass": "MULTI_REGIONAL",
    //       "size": "777835",
    //       "md5Hash": "nTd7EM53jEk4s8fixjoimg==",
    //       "crc32c": "ixxYVw==",
    //       "etag": "CNCxnqeE3+ECEAE=",
    //       "timeCreated": "2019-04-20T15:55:49.593Z",
    //       "updated": "2019-04-20T15:55:49.593Z",
    //       "timeStorageClassUpdated": "2019-04-20T15:55:49.593Z"
    //     },
    //     {
    //       "kind": "storage#object",
    //       "id": "chilkat-ocean/starfish.jpg/1628958192639968",
    //       "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/starfish.jpg",
    //       "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/starfish.jpg?generation=1628958192639968&alt=media",
    //       "name": "starfish.jpg",
    //       "bucket": "chilkat-ocean",
    //       "generation": "1628958192639968",
    //       "metageneration": "1",
    //       "contentType": "image/jpeg",
    //       "storageClass": "MULTI_REGIONAL",
    //       "size": "6229",
    //       "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
    //       "crc32c": "9RjgwQ==",
    //       "etag": "COC/tJP2sPICEAE=",
    //       "timeCreated": "2021-08-14T16:23:12.660Z",
    //       "updated": "2021-08-14T16:23:12.660Z",
    //       "timeStorageClassUpdated": "2021-08-14T16:23:12.660Z"
    //     }
    //   ]
    // }

    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    var id: String?
    var selfLink: String?
    var mediaLink: String?
    var name: String?
    var bucket: String?
    var generation: String?
    var metageneration: String?
    var contentType: String?
    var storageClass: String?
    var size: String?
    var md5Hash: String?
    var crc32c: String?
    var etag: String?
    var timeCreated: String?
    var updated: String?
    var timeStorageClassUpdated: String?

    var kind: String? = json.string(of: "kind")
    var i: Int = 0
    var count_i: Int = json.size(ofArray: "items").intValue
    while i < count_i {
        json.i = i
        kind = json.string(of: "items[i].kind")
        id = json.string(of: "items[i].id")
        selfLink = json.string(of: "items[i].selfLink")
        mediaLink = json.string(of: "items[i].mediaLink")
        name = json.string(of: "items[i].name")
        bucket = json.string(of: "items[i].bucket")
        generation = json.string(of: "items[i].generation")
        metageneration = json.string(of: "items[i].metageneration")
        contentType = json.string(of: "items[i].contentType")
        storageClass = json.string(of: "items[i].storageClass")
        size = json.string(of: "items[i].size")
        md5Hash = json.string(of: "items[i].md5Hash")
        crc32c = json.string(of: "items[i].crc32c")
        etag = json.string(of: "items[i].etag")
        timeCreated = json.string(of: "items[i].timeCreated")
        updated = json.string(of: "items[i].updated")
        timeStorageClassUpdated = json.string(of: "items[i].timeStorageClassUpdated")
        i = i + 1
    }


}