google drive api v3 update description

回眸只為那壹抹淺笑 提交于 2021-02-10 22:18:28

问题


google drive api v3

I need to change the description of an existing object. How do I change just that, leaving the rest unchanged?

I am doing by analogy with python. I get the file metadata, add the "description" line to it and execute update. There are no errors, the description simply does not change

I don't know much about js, I will be glad if you can help me.

thanks

function updateFile(fileId) {

  var request = gapi.client.drive.files.get({
    'fileId': fileId
  });
  request.execute(function(resp) {
    var data = resp;
    //data = data['result']
    data['description'] = "433434"
  });

  var fileMetadata = data;

  const boundary = '-------314159265358979323846';
  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';
    // Updating the metadata is optional and you can instead use the value from drive.files.get.
    var base64Data = btoa(reader.result);
    var multipartRequestBody =
      delimiter +
      'Content-Type: application/json\r\n\r\n' +
      JSON.stringify(fileMetadata) +
      delimiter +
      'Content-Type: ' + contentType + '\r\n' +
      'Content-Transfer-Encoding: base64\r\n' +
      '\r\n' +
      base64Data +
      close_delim;

    var request = gapi.client.request({
      'path': '/upload/drive/v3/files/' + fileId,
      'method': 'PATCH',
      'params': {'uploadType': 'multipart', 'alt': 'json'},
      'headers': {
        'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
      },
      'body': multipartRequestBody
    });
    if (!callback) {
      callback = function(file) {
        console.log(file)
      };
    }
    request.execute(callback);
  };
}

回答1:


Thank you all, I figured out my problem. Here is the code, it works for me)

function updateFile(fileId) {
    var request = gapi.client.drive.files.get({
      'fileId': fileId
    });
    request.execute(function(resp) {
      fileMetadata = resp;
      fileMetadata['description'] = "new_description";
      delete fileMetadata.id;
      var request = gapi.client.request({
        'path': '/drive/v3/files/' + fileId,
        'method': 'PATCH',
        'body': fileMetadata});
      request.execute(function(resp) {
      });
    })
  }


来源:https://stackoverflow.com/questions/63430882/google-drive-api-v3-update-description

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!