Dart
Dart
Initiate Resumable Upload Session
See more Google Cloud Storage Examples
Initiate a Google Cloud Storage resumable upload session..Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final http = CkHttp();
final jsonToken = CkJsonObject();
try {
jsonToken.loadFile('qa_data/tokens/googleCloudStorage.json');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final jsonMetaData = 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');
final url = 'https://storage.googleapis.com/upload/storage/v1/b/{\$bucket_name}/o?uploadType=resumable&name={\$object_name}';
final resp = CkHttpResponse();
try {
http.httpJson('POST', url, jsonMetaData, 'application/json', resp);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final statusCode = resp.statusCode;
print('response status code = $statusCode');
var sessionUrl = '';
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('Location');
print('Session URL = $sessionUrl');
}
}