How to rename file with google drive api v3? electron, nodejs

Viewed 2529

for v2, we can get an example how to rename file with google drive API. here's the link https://developers.google.com/drive/api/v2/reference/files/patch#examples
here's how we can rename a file with v2 in javascript

/**
 * Rename a file.
 *
 * @param {String} fileId <span style="font-size: 13px; ">ID of the file to rename.</span><br> * @param {String} newTitle New title for the file.
 */
function renameFile(fileId, newTitle) {
  var body = {'title': newTitle};
  var request = gapi.client.drive.files.patch({
    'fileId': fileId,
    'resource': body
  });
  request.execute(function(resp) {
    console.log('New Title: ' + resp.title);
  });
}

i need to create function like example from v2 with electron and nodejs. here's what i've done so far mfm-gdrive

1 Answers

If you want to rename a file using the Drive API v3, you will have to use the Files:update request, like this:

function renameFile(auth) {
  const drive = google.drive({version: 'v3', auth});
  var body = {'name': 'NEW_NAME'};
  drive.files.update({
    fileId: 'ID_OF_THE_FILE',
    resource: body,
  }, (err, res) => {
    if (err) return console.log('The API returned an error: ' + err);
    else {
      console.log('The name of the file has been updated!');
    }
  });
}

You can also simulate the update request by using the Drive API v3 Reference here.

As for examples, I suggest you check the Drive API v3 Node.js Quickstart here which you can later adapt such that is suits your needs accordingly.

Reference

Related