【问题标题】:How I can copy folder in google drive api?如何在 google drive api 中复制文件夹?
【发布时间】:2020-12-08 11:05:06
【问题描述】:

文档描述了如何复制一个文件 (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))
              })
            })
          })

我尝试了在 Internet 上找到的其他解决方案,但它们也不起作用。您能否附上一些适合您的示例代码?

【问题讨论】:

    标签: javascript google-drive-api


    【解决方案1】:

    files.copy 只是复制一个文件

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

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

    文件夹方法中没有批量复制所有内容。您将不得不一一完成。

    【讨论】:

    • 谢谢!但是你能告诉我在这种情况下如何正确复制循环吗?我已经更新了我自己尝试做的描述。但是如果我复制的文件夹中有其他文件夹,那么它就不能正常工作。
    • 抱歉,这不是编码服务。如果您有任何问题,请尝试打开一个新问题。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多