I tried a lot to get this thing done but all in vain.
Here is complete documentation
Link to JavaScript code base
If I try Google\'s online tool to
I tried a lot but unable to add metadata as part of initial file upload request. I ended up sending metadata in another 'patch' request. Please let me know if you have a better solution
/**
* Google Cloud Storage API request to insert an object into
* your Google Cloud Storage bucket.
*/
function insertObject(fileControl, metadata, callBack) {
debugger;
try{
var fileData = fileControl.files[0];
}
catch(e) {
//'Insert Object' selected from the API Commands select list
//Display insert object button and then exit function
//filePicker.style.display = 'block';
return;
}
const boundary = 'hoho314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var fileName = metadata.name;
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e) {
var contentType = fileData.type || 'application/octet-stream';
var metadata1 = {
'name': fileName,
'mimeType': contentType
};
var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type: application/json; charset=UTF-8 \r\n\r\n' +
JSON.stringify(metadata1) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
//Note: gapi.client.storage.objects.insert() can only insert
//small objects (under 64k) so to support larger file sizes
//we're using the generic HTTP request method gapi.client.request()
var request = gapi.client.request({
'path': '/upload/storage/' + API_VERSION + '/b/' + PCSING_BUCKET + '/o',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/related; boundary="' + boundary + '"'
},
'body': multipartRequestBody});
try{
//Execute the insert object request
request.execute(function(resp) {
multipartRequestBody = {
'metadata': metadata
}
request = gapi.client.request({
'path': '/storage/' + API_VERSION + '/b/' + PCSING_BUCKET + '/o/' + fileName,
'method': 'PATCH',
'body': multipartRequestBody
});
//https://www.googleapis.com/storage/v1/b/bucket/o/object
request.execute(function(resp) {
callBack();
console.log(resp);
});
});
//Store the name of the inserted object
//object = fileData.name;
}
catch(e) {
alert('An error has occurred: ' + e.message);
}
}
}
I struggled with this one without finding a good resource, so it was all trial and error.
The answer appears to be putting the additional metadata in a metadata key:
var metadata1 = {
metadata: fileData.metadata,
'name': fileData.name,
'mimeType': contentType
};
Sorta meta