Google Drive Rest API-如何检查文件是否已更改 [英] Google Drive Rest API - How to check if file has changed

查看:81
本文介绍了Google Drive Rest API-如何检查文件是否已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一种可靠的方法,除了比较完整内容外,还可以检查文件是否在云端硬盘中已更新/更改?

我一直在为此苦苦挣扎.这是我尝试过的两件事:

1.文件version

我将纯文本文件上传到Google云端硬盘(简单上传更新端点),然后保存version成功上传后返回的文件元数据. >

然后我轮询Drive API(获取端点 )偶尔检查版本是否已更改.

麻烦的是,在上传文件的一两秒钟内,该版本再次遭到破坏.

文件内容没有更改.该文件尚未打开,查看,甚至没有下载到其他任何地方.不过,版本号比上传后的版本有所增加.

对于我的代码,此版本号更改表示远程文件已在云端硬盘中更改,因此它将下载新版本.每次!

2. Changes端点

我尝试使用更改api . /p>

上传文件后,我使用 changes.getStartPageToken changes.list .

稍后,我将使用此page token来轮询Changes API的更改,并过滤上载文件的fileId的更改.轮询更改时,我使用以下选项:

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

再一次,存在与版本号相同的问题.上传文件后立即返回的页面令牌在一两秒钟内再次更改.新页面令牌显示上传的文件已更改.

同样,文件内容没有更改.它尚未在其他任何地方打开,更新或下载.未与其他任何人共享.

但是,上传几秒钟后,该文件重新出现在更改列表中.

结果,本地代码假设进行了远程更改,然后从云端硬盘重新下载了文件.


可能的解决方法

作为一个棘手的钩子,我可以在文件上传后等待几秒钟,然后再获取新的文件版本/更改页令牌.这可以解决延迟的版本增加问题.

但是,没有文档说明导致此幻像版本号更改的原因(或changes.list).因此,我不确定是否知道:

  1. 等待多长时间才能安全地获得已解决的"版本号,而不会丢失其他用户/应用程序可能进行的更改?
  2. 新的(延迟的)版本号是否稳定,或者可能随时无故再次更改?


是否存在一种可靠的方法,除了比较完整内容外,还可以检查文件是否在云端硬盘中已更新/更改?

解决方案

您可以尝试使用版本API .

修订资源对象也具有md5Checksum属性

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?

解决方案

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.

这篇关于Google Drive Rest API-如何检查文件是否已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆