Warm tip: This article is reproduced from serverfault.com, please click

How can i move the file or folder to another folder in google drive api?

发布于 2020-12-03 14:17:08

I did everything as in the documentation (https://developers.google.com/drive/api/v3/folder#inserting_a_file_in_a_folder). But that doesn't work for me. I have corrected this script a bit:

window.gapi.client.drive.files.get({
        fileId: fileId,
        fields: 'parents'
      }).then(res => {
        console.log(res)
        window.gapi.client.drive.files.update({
          fileId: this.fileData.id,
          addParents: folderId,
          removeParents: res.result.parents[0],
          fields: 'id, parents'
        }).then(res => {
          console.log(res)
        })
      })

It now moves the file to a different location, but does not delete the current location. That is, after working out my code, it is like copying a file, and not moving it.

Questioner
Mikhail
Viewed
0
ale13 2020-12-10 19:12:03

The code snippet you are using removes only the first parent.

In order to remove all the parents correctly you will have to add the following line to your code:

var previousParents = res.result.parents.join(',');

And when calling the update method, you will have to remove previousParents:

removeParents: previousParents,

Reference