Search code examples
javascriptgoogle-drive-api

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?


Solution

  • files.copy does just that copies a single file

    enter image description here

    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.