无法使用nodeJs库重命名Google云端硬盘文件

时间:2017-07-16 15:14:20

标签: node.js google-api google-drive-api

我无法在NodeJs应用中重命名Google云端硬盘中的文件。我正在使用:

  • googleapi nodeJs library v。20.1.0
  • node v。8.1.4

该应用正在处理文件请求,并且能够复制文件。但是,在更新它们时,永远不会应用新的name

我确实得到了文件资源的响应,这需要the request was successful

我这样做:

const google = require( 'googleapis' );
let googleService = google.drive( 'v3' );

let params = {
  auth: testedAndValidOAuth,
  fileId: validFileId,
  uploadType: 'multipart',
  name: "some name 2"
};

googleService.files.update( params, ( err, response ) => console.log(response));

对于属性uploadType,我尝试使用和不使用所有可用选项:resumablemultipartmedia。尝试media时出现以下错误:

Error: The API returned an error: Error: Uploads must be sent to the upload URL. Re-send this request to https://www.googleapis.com/upload/drive/v3/files/xxxfileIdxxx?uploadType=media&name=some+name+2

我试图手动编辑库中的链接,但错误消失了,但遗憾的是名称没有改变。

在图书馆看来,我注意到没有提到name param:

@param {object} params Parameters for request
@param {string=} params.addParents A comma-separated list of parent IDs to add.
@param {string} params.fileId The ID of the file.
@param {boolean=} params.keepRevisionForever Whether to set the 'keepForever' field in the new head revision. This is only applicable to files with binary content in Drive.
@param {string=} params.ocrLanguage A language hint for OCR processing during image import (ISO 639-1 code).
@param {string=} params.removeParents A comma-separated list of parent IDs to remove.
@param {boolean=} params.supportsTeamDrives Whether the requesting application supports Team Drives.
@param {boolean=} params.useContentAsIndexableText Whether to use the uploaded content as indexable text.
@param  {object} params.resource Media resource metadata
@param {object} params.media Media object
@param {string} params.media.mimeType Media mime-type
@param {string|object} params.media.body Media body contents
@param {object} [options] Optionally override request options, such as `url`, `method`, and `encoding`.
@param {callback} callback The callback that handles the response.

所以我想知道,我做错了什么,这是有意的,这意味着我们无法编辑文件名,还是库中的错误?

1 个答案:

答案 0 :(得分:1)

尝试在params.media.body中设置新名称,例如

params.resource = {name: "newname"}

根据评论更正