简体   繁体   中英

How I can copy folder in google drive api?

The documentation describes how to copy one file (https://developers.google.com/drive/api/v2/reference/files/copy ), but nothing is said about the folder. This script, when copying a folder, gives an error 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))
              })
            })
          })

I tried other solutions that I found on the Internet, but they also did not work. Could you please attach some sample code that works for you?

files.copy does just that copies a single file

在此处输入图像描述

If you check the very top of the documentation page that you linked you will notice it states it does not work on folders.

To copy a folder you should first do a file.create and create a folder the do a file.list to loop though all of the files in the folder then do a file.copy on each of the files.

There is no bulk copy everything in a folder method. Your going to have to do this one by one.

I've published a pypi package to copy a google drive folder. It recursively copies files down the file directory from a folder you specified

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM