将附加元数据作为文件上传请求的一部分上传到Google Cloud Storage [英] Uploading additional metadata as part of file upload request to Google Cloud Storage

查看:144
本文介绍了将附加元数据作为文件上传请求的一部分上传到Google Cloud Storage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了完成这件事,我尝试了很多,但都是徒劳的。



此处是完整的文档



工具上传文件,然后成功创建我提供的任何元数据。我不知道他们在做什么不同。

我最新的代码库和元数据一起上传文件

< blockquote>

函数insertObject(event){

  try {
var fileData = event。 target.files [0];
catch {e} {
//从API选择的'Insert Object'选择列表
//显示插入对象按钮,然后退出函数
filePicker .style.display ='block';
return;
}

const boundary ='hoho314159265358979323846';
const delimiter =\r\\\
--+ boundary +\r\\\
;
const close_delim =\r\\\
--+ boundary + - ;

var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e){
var contentType = fileData.type || 应用程序/八位字节流;
var metadata = {
'name':fileData.name,
'mimeType':contentType,
'test':contentType
};

var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type:application / json; \r\\\
\r\\\
'+
JSON.stringify(元数据)+
分隔符+
'Content-Type:'+ contentType +'\r\ n'+
'Content-Transfer-Encoding:base64 \\\\\''''''''$ $ b'\r\''+
base64Data +
close_delim;

//注意:gapi.client.storage.objects.insert()只能插入
//小对象(小于64k)以支持更大的文件大小
//我们使用通用的HTTP请求方法gapi.client.request()
var request = gapi.client.request({$ b $'path':'/ upload / storage /'+ API_VERSION +'/ $'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'
'Content-Type':'multipart / related; boundary =''+ boundary +''
},
'body':multipartRequestBody});
//移除主要内容中的当前API结果项div
listChildren = document.getElementById('main-content')。childNodes;
if(listChildren.length> 1){
listChildren [1] .parentNode.removeChild(listChildren [1]);

try {
//执行插入对象请求
executeRequest(request,'insertObject');
//存储插入对象的名称
object = fileData.name;
}
catch(e){
alert('发生错误:'+ e.message);







$ b $ / p>我已阅读多部分文档,并试图做同样的事情,但没有帮助。

如果我创建元数据(以json格式)像下面那么它会抛出错误代码400说

var metadata = {
'metadata':{
'customerName':'Sigma1',
'model':'xvrt56',
'issue':'loud sound'
}
};

解决方案

我尝试了很多,但无法添加元数据作为初始文件上传请求的一部分。我最终发送了另一个补丁请求的元数据。请让我知道,如果你有一个更好的解决方案

  / ** 
* Google Cloud Storage API请求插入将其存入
*您的Google云端存储分区。
* /
函数insertObject(fileControl,元数据,callBack){
调试器;
尝试{
var fileData = fileControl.files [0];
catch(e){
//从API选择的'Insert Object'选择列表
//显示插入对象按钮,然后退出函数
/ /filePicker.style.display ='block';
return;
}
const boundary ='hoho314159265358979323846';
const delimiter =\r\\\
--+ boundary +\r\\\
;
const close_delim =\r\\\
--+ boundary + - ;

var fileName = metadata.name;

var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e){
var contentType = fileData.type || 应用程序/八位字节流;
var metadata1 = {
'name':fileName,
'mimeType':contentType
};

var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type:application / json; charset = UTF-8 \r\\\
\r\\\
'+
JSON.stringify(metadata1)+
delimiter +
'Content-Type:'+ contentType +' \r\\\
'+
'Content-Transfer-Encoding:base64 \\\\''+
'\r\''+
base64Data +
close_delim;

//注意:gapi.client.storage.objects.insert()只能插入
//小对象(小于64k)以支持更大的文件大小
//我们使用通用的HTTP请求方法gapi.client.request()
var request = gapi.client.request({$ b $'path':'/ upload / storage /'+ API_VERSION +'/ $'$'$'$'$'$'$'$'$'$'$'$'$'''','$'
'Content-Type':'multipart / related; boundary =''+ boundary +''
},
'body':multipartRequestBody});

try {
//执行插入对象请求
request.execute(function(resp){

multipartRequestBody = {
'元数据':元数据
}

request = gapi.client.request({$ b $'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);
});

});

//存储插入对象的名称
// object = fileData.name;
}
catch(e){
alert('发生错误:'+ e.message);
}
}
}


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 upload file then it successfully creates whatever metadata I supply. 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) {

  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.

var metadata = { 'metadata': { 'customerName': 'Sigma1', 'model': 'xvrt56', 'issue': 'loud sound' } };

解决方案

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);
        }
      }
    }

这篇关于将附加元数据作为文件上传请求的一部分上传到Google Cloud Storage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆