Sample code for 30+ languages & platforms
JavaScript

Initiate Resumable Upload Session

See more Google Cloud Storage Examples

Initiate a Google Cloud Storage resumable upload session..
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var http = new CkHttp();

var jsonToken = new CkJsonObject();
success = jsonToken.LoadFile("qa_data/tokens/googleCloudStorage.json");
if (success == false) {
    console.log(jsonToken.LastErrorText);
    return;
}

var jsonMetaData = new CkJsonObject();
jsonMetaData.UpdateString("contentType","image/jpeg");

// Adds the "Authorization: Bearer <access_token>" header..
http.AuthToken = jsonToken.StringOf("access_token");

http.SetUrlVar("bucket_name","chilkat-bucket-b");
http.SetUrlVar("object_name","penguins2.jpg");
var url = "https://storage.googleapis.com/upload/storage/v1/b/{$bucket_name}/o?uploadType=resumable&name={$object_name}";
var resp = new CkHttpResponse();
success = http.HttpJson("POST",url,jsonMetaData,"application/json",resp);
if (success == false) {
    console.log(http.LastErrorText);
    return;
}

var statusCode = resp.StatusCode;
console.log("response status code = " + statusCode);

var sessionUrl = "";

if (statusCode !== 200) {
    console.log(resp.BodyStr);
}
else {
    // The session URL will be used to upload the file in chunks, in subsequent HTTP POSTs...
    sessionUrl = resp.GetHeaderField("Location");
    console.log("Session URL = " + sessionUrl);
}