Sample code for 30+ languages & platforms
Swift

Google Cloud Storage: Update Object Metadata

See more Google Cloud Storage Examples

Demonstrates how to update (edit) the metadata associated with an object in a Google Cloud Storage bucket.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let http = CkoHttp()!

    // Implements the following CURL command:

    // curl -X PATCH --data-binary @JSON_FILE_NAME \
    //   -H "Authorization: Bearer OAUTH2_TOKEN" \
    //   -H "Content-Type: application/json" \
    //   "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME"

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    let bdRequestBody = CkoBinData()!
    success = bdRequestBody.loadFile(path: "JSON_FILE_PATH")
    if success != true {
        print("Failed to load JSON_FILE_PATH")
        return
    }

    // Adds the "Authorization: Bearer OAUTH2_TOKEN" header.
    http.authToken = "OAUTH2_TOKEN"

    let resp = CkoHttpResponse()!
    success = http.httpBd(verb: "PATCH", url: "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME", bd: bdRequestBody, contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    let sbResponseBody = CkoStringBuilder()!
    resp.getBodySb(sb: sbResponseBody)

    let jResp = CkoJsonObject()!
    jResp.loadSb(sb: sbResponseBody)
    jResp.emitCompact = false

    print("Response Body:")
    print("\(jResp.emit()!)")

    var respStatusCode: Int = resp.statusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp.header!)")
        print("Failed.")
        return
    }


}