React Native
React Native
Initiate Resumable Upload Session
See more Google Cloud Storage Examples
Initiate a Google Cloud Storage resumable upload session..Chilkat React Native Downloads
import { Http, HttpResponse, JsonObject } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = new Http();
const jsonToken = new JsonObject();
try {
jsonToken.loadFile('qa_data/tokens/googleCloudStorage.json');
} catch {
console.log(jsonToken.lastErrorText);
return;
}
const jsonMetaData = new JsonObject();
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');
const url = 'https://storage.googleapis.com/upload/storage/v1/b/{$bucket_name}/o?uploadType=resumable&name={$object_name}';
const resp = new HttpResponse();
try {
await http.httpJsonAsync('POST', url, jsonMetaData, 'application/json', resp);
} catch {
console.log(http.lastErrorText);
return;
}
const statusCode = resp.statusCode;
console.log(`response status code = ${statusCode}`);
let 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}`);
}
}