[英]Uploading additional metadata as part of file upload request to Google Cloud Storage
I tried a lot to get this thing done but all in vain. 我花了很多力气把事情做好,但徒劳无功。
Here is complete documentation 这是完整的文档
Link to JavaScript code base 链接到JavaScript代码库
If I try Google's online tool to upload file then it successfully creates whatever metadata I supply. 如果我尝试使用Google的在线工具上传文件,那么它会成功创建我提供的任何元数据。 I am not sure what different they are doing.
我不确定他们在做什么。 Unfortunately, I can't even figure it out.
不幸的是,我什至无法弄清楚。
My latest code base to upload a file along with metadata 我最新的代码库,用于与元数据一起上传文件
function insertObject(event) {
函数insertObject(event){
try{ var fileData = event.target.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 reader = new FileReader(); reader.readAsBinaryString(fileData); reader.onload = function(e) { var contentType = fileData.type || 'application/octet-stream'; var metadata = { 'name': fileData.name, 'mimeType': contentType, 'test': contentType }; var base64Data = btoa(reader.result); var multipartRequestBody = delimiter + 'Content-Type: application/json; \\r\\n\\r\\n' + JSON.stringify(metadata) + 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/' + BUCKET + '/o', 'method': 'POST', 'params': {'uploadType': 'multipart'}, 'headers': { 'Content-Type': 'multipart/related; boundary="' + boundary + '"' }, 'body': multipartRequestBody}); //Remove the current API result entry in the main-content div listChildren = document.getElementById('main-content').childNodes; if (listChildren.length > 1) { listChildren[1].parentNode.removeChild(listChildren[1]); } try{ //Execute the insert object request executeRequest(request, 'insertObject'); //Store the name of the inserted object object = fileData.name; } catch(e) { alert('An error has occurred: ' + e.message); } } }
I have read multipart documentation and tried to do the same thing but no help. 我已经阅读了多部分文档,并尝试做同样的事情,但没有帮助。
If I create metadata (in json format) like following then it throws error code 400 saying object required otherwise it uploads file but not metadata. 如果我像下面那样创建元数据(以json格式),那么它将抛出错误代码400,表明需要对象,否则它将上传文件,但不上传元数据。
var metadata = { 'metadata': { 'customerName': 'Sigma1', 'model': 'xvrt56', 'issue': 'loud sound' } }; var元数据= {'元数据':{'customerName':'Sigma1','model':'xvrt56','issue':'响亮的声音}};
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 排序元
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.