Sample code for 30+ languages & platforms
Swift

Initiate Resumable Upload Session

See more Google Cloud Storage Examples

Initiate a Google Cloud Storage resumable upload session..

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()!

    let jsonToken = CkoJsonObject()!
    success = jsonToken.loadFile(path: "qa_data/tokens/googleCloudStorage.json")
    if success == false {
        print("\(jsonToken.lastErrorText!)")
        return
    }

    let jsonMetaData = CkoJsonObject()!
    jsonMetaData.updateString(jsonPath: "contentType", value: "image/jpeg")

    // Adds the "Authorization: Bearer <access_token>" header..
    http.authToken = jsonToken.string(of: "access_token")

    http.setUrlVar(name: "bucket_name", value: "chilkat-bucket-b")
    http.setUrlVar(name: "object_name", value: "penguins2.jpg")
    var url: String? = "https://storage.googleapis.com/upload/storage/v1/b/{$bucket_name}/o?uploadType=resumable&name={$object_name}"
    let resp = CkoHttpResponse()!
    success = http.httpJson(verb: "POST", url: url, json: jsonMetaData, contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    var statusCode: Int = resp.statusCode.intValue
    print("response status code = \(statusCode)")

    var sessionUrl: String? = ""

    if statusCode != 200 {
        print("\(resp.bodyStr!)")
    }
    else {
        // The session URL will be used to upload the file in chunks, in subsequent HTTP POSTs...
        sessionUrl = resp.getHeaderField(fieldName: "Location")
        print("Session URL = \(sessionUrl!)")
    }


}