将其他元数据作为文件上传请求的一部分上传到 Google 云存储
Uploading additional metadata as part of file upload request to Google Cloud Storage
为了完成这件事我做了很多尝试,但都是徒劳。
Here 是完整的文档
Link 到 JavaScript 代码库
如果我尝试 Google 的在线 tool 上传文件,那么它会成功创建我提供的任何元数据。我不确定他们在做什么。不幸的是,我什至无法弄清楚。
我上传文件和元数据的最新代码库
function 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);
}
}
}
我已阅读 multipart documentation 并尝试做同样的事情但没有帮助。
如果我像下面这样创建元数据(json 格式),它会抛出错误代码 400,说明需要对象,否则它会上传文件而不是元数据。
var 元数据 = {
'metadata':{
'customerName': 'Sigma1',
'model': 'xvrt56',
'issue': 'loud sound'
}
};
我尝试了很多但无法将元数据添加为初始文件上传请求的一部分。我最终在另一个 'patch' 请求中发送了元数据。如果您有更好的解决方案,请告诉我
/**
* 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);
}
}
}
我在没有找到好的资源的情况下苦苦挣扎,所以这一切都是反复试验。
答案似乎是将附加元数据放入元数据键中:
var metadata1 = {
metadata: fileData.metadata,
'name': fileData.name,
'mimeType': contentType
};
有点元
为了完成这件事我做了很多尝试,但都是徒劳。
Here 是完整的文档
Link 到 JavaScript 代码库
如果我尝试 Google 的在线 tool 上传文件,那么它会成功创建我提供的任何元数据。我不确定他们在做什么。不幸的是,我什至无法弄清楚。
我上传文件和元数据的最新代码库
function 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); } } }
我已阅读 multipart documentation 并尝试做同样的事情但没有帮助。
如果我像下面这样创建元数据(json 格式),它会抛出错误代码 400,说明需要对象,否则它会上传文件而不是元数据。
var 元数据 = { 'metadata':{ 'customerName': 'Sigma1', 'model': 'xvrt56', 'issue': 'loud sound' } };
我尝试了很多但无法将元数据添加为初始文件上传请求的一部分。我最终在另一个 'patch' 请求中发送了元数据。如果您有更好的解决方案,请告诉我
/**
* 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);
}
}
}
我在没有找到好的资源的情况下苦苦挣扎,所以这一切都是反复试验。
答案似乎是将附加元数据放入元数据键中:
var metadata1 = {
metadata: fileData.metadata,
'name': fileData.name,
'mimeType': contentType
};
有点元