对于v2,我们可以获得如何使用google驱动器API重命名文件的示例。这是链接https://developers.google.com/drive/api/v2/reference/files/patch#examples
以下是如何在javascript 中使用v2重命名文件
/**
* 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);
});
}
我需要使用electron和nodejs从v2创建类似函数的示例。以下是我到目前为止所做的mfm gdrive
如果您想使用Drive API v3重命名文件,则必须使用Files:update
请求,如下所示:
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!');
}
});
}
您也可以使用这里的驱动器API v3参考来模拟update
请求。
例如,我建议您在这里查看Drive API v3 Node.js Quickstart,稍后您可以对其进行调整,以满足您的需求。
参考
驱动API v3文件:更新;
驱动器API v3文件和文件夹概述;
驱动API v3 Node.js快速启动。