Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive Rest API - How to check if file has changed

Is there a reliable way, short of comparing full contents, of checking if a file was updated/change in Drive?

I have been struggling with this for a bit. Here's the two things I have tried:

1. File version number

I upload a plain text file to Google Drive (simple upload, update endpoint), and save the version from the file metadata returned after a successful upload.

Then I poll the Drive API (get endpoint) occasionally to check if the version has changed.

The trouble is that within a second or two of uploading the file, the version gets bumped up again.

There are no changes to the file content. The file has not been opened, viewed, or even downloaded anywhere else. Still, the version number increases from what it was after the upload.

To my code this version number change indicates that the remote file has been changed in Drive, so it downloads the new version. Every time!

2. The Changes endpoints

As an alternative I tried using the Changes api.

After I upload the file, I get a page token using changes.getStartPageToken or changes.list.

Later I use this page token to poll the Changes API for changes, and filter the changes for the fileId of uploaded file. I use these options when polling for changes:

{
    "includeRemoved": false
    "restrictToMyDrive": true
    "spaces": "drive"
}

Here again, there is the same problem as with the version number. The page token returned immediately after uploading the file changes again within a second or two. The new page token shows the uploaded file having been changed.

Again, there is no change to the content of the file. It hasn't been opened, updated, downloaded anywhere else. It isn't shared with anyone else.

Yet, a few seconds after uploading, the file reappears in the changes list.

As a result, the local code redownloads the file from Drive, assuming remote changes.


Possible workaround

As a hacky hook, I could wait a few seconds after the file upload before getting the new file-version/changes-page-token. This may take care of the delayed version increment issue.

However, there is no documentation of what is causing this phantom change in version number (or changes.list). So, I have no sure way of knowing:

  1. How long a wait is safe enough to get a 'settled' version number without losing possible changes by other users/apps?
  2. Whether the new (delayed) version number will be stable, or may change again at any time for no reason?

Is there a reliable way, short of comparing full contents, of checking if a file was updated/change in Drive?

like image 922
Adi B Avatar asked Mar 04 '23 11:03

Adi B


1 Answers

You can try using the md5Checksum property of the File resource object, if your file is not a Google Doc file (ie. binary). You should be able to use that to track changes to the contents of your binary files.

You might also be able to use the Revisions API.

The Revisions resource object also has a md5Checksum property.

like image 79
TheAddonDepot Avatar answered Mar 15 '23 04:03

TheAddonDepot