如何复制 google 驱动器 api 中的文件夹?

How I can copy folder in google drive api?

文档描述了如何复制一个文件 (https://developers.google.com/drive/api/v2/reference/files/copy),但没有提及文件夹。此脚本在复制文件夹时出现错误 403:

window.gapi.client.drive.files
          .create({
            resource: {
              name: this.currentFile.newTitle,
              mimeType: 'application/vnd.google-apps.folder'
            },
            fields: 'id, name, mimeType, createdTime'
          }).then(res => {
            window.gapi.client.drive.files.list({
              q: `'${this.currentFile.id}' in parents and trashed = false`,
              fields: 'files(id, name, createdTime, mimeType)'
            }).then(res => {
              console.log(res)
              res.result.files.forEach(file => {
                window.gapi.client.drive.files
                  .copy({
                    fileId: file.id,
                    fields: 'id, name, mimeType, createdTime'
                  }).then(res => console.log(res))
              })
            })
          })

我尝试了在网上找到的其他解决方案,但都没有用。您能否附上一些适合您的示例代码?

files.copy 只是复制一个文件

如果您查看链接的文档页面的最顶部,您会注意到它声明它不适用于文件夹。

要复制一个文件夹,您应该首先执行 file.create 并创建一个文件夹,然后执行 file.list 以遍历文件夹中的所有文件,然后执行 file.copy每个文件。

文件夹方法中没有批量复制所有内容。您将不得不一项一项地执行此操作。

我发布了一个 pypi 包来复制 google 驱动器文件夹。它递归地将文件从您指定的文件夹复制到文件目录

https://pypi.org/project/googledrive-cloner/