Home > Net >  Changing a file's parent ID using Google Drive's API causes a new empty revision to be cre
Changing a file's parent ID using Google Drive's API causes a new empty revision to be cre

Time:08-10

Executing the command bellow on a any binary file causes the file to be moved as intended:

curl \
--silent \
--fail \
-H 'GData-Version: 3.0' \
-H "Authorization: Bearer $ACCESS_TOKEN" \
--request PATCH \
"https://www.googleapis.com/upload/drive/v3/files/$FILE_ID\
?supportsAllDrives=true\
&supportsTeamDrives=true\
&removeParents=$OLD_PARENT_ID\
&addParents=$NEW_PARENT_ID"

However this also creates a new revision where the file's size is zero and the file can't be downloaded or previewed.

Removing that new empty revision seems to solve the problem and preserves the file's new path.

I suspected that it might be because the folders are shared and different, but trying it on a file on the same Drive reproduced the issue.

According to API's reference for files parents:

Update requests must use the addParents and removeParents parameters to modify the parents list.

Am I using the API correctly here?

CodePudding user response:

When I saw your curl command, you are using the endpoint of https://www.googleapis.com/upload/drive/v3/files/$FILE_ID. This endpoint is used for uploading a file's content and file metadata. But, in your request, the file content is not included. I thought that the reason for your current issue of However this also creates a new revision where the file's size is zero and the file can't be downloaded or previewed. is due to this.

When the file is moved from a current folder to another folder without updating the file content, you can use the request of PATCH https://www.googleapis.com/drive/v3/files/fileId.

When this is reflected in your showing curl command, it becomes as follows.

Modified curl command:

curl \
--silent \
--fail \
-H 'GData-Version: 3.0' \
-H "Authorization: Bearer $ACCESS_TOKEN" \
--request PATCH \
"https://www.googleapis.com/drive/v3/files/$FILE_ID\
?supportsAllDrives=true\
&supportsTeamDrives=true\
&removeParents=$OLD_PARENT_ID\
&addParents=$NEW_PARENT_ID"

Reference:

  • Related