如何检查Google Drive文件夹的内容是否已更改? [英] How to check if content of google drive folder has changed?

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

问题描述

我想对文件夹内容进行缓存,因此,如果我能以某种方式获取Google驱动器中的文件夹已更改的信息,然后刷新缓存,那就太酷了.有办法吗?最好用php.

I would like to do caching for folder content, therefore it will be cool if I can somehow get an information if folder in google drive changed and then flush the cache. Is there a way for that? Best in php.

推荐答案

一段时间后,我想到了一个不错的解决方案:

After some time I came up with this nice solution:

function isGoogleDriveFolderContentUpdated($lastTime, $folderId, &$client)
{

    $appsactivityService = new Google_Service_Appsactivity($client);

    $optParams = array(
        'source'           => 'drive.google.com',
        'drive.ancestorId' => $folderId,
        'pageSize'         => 1,
    );

    $results = $appsactivityService->activities->listActivities($optParams);

    if (count($results->getActivities()) == 0) {

        return 0;

    } else {
        $activities = $results->getActivities();
        $activity = $activities[0];

        $event = $activity->getCombinedEvent();
        $activityTime = date(DateTime::RFC3339, $event->getEventTimeMillis() / 1000);

        $lastTime = strtotime($lastTime);
        $activityTime = strtotime($activityTime);

        if ($activityTime > $lastTime) {
            //folder content changed since last check
            return 1;
        }

        return 0;
    }

}

可以这样使用:

echo isGoogleDriveFolderContentUpdated("2018-11-26T21:03:57+01:00" ,"1A9CXgB44F1khfDAzU4t0R322TWh", $client);

其中第一个参数是最后检查的日期时间,第二个Google驱动器文件夹ID和客户端变量. 此外,您的应用程序应具有以下作用域:https://www.googleapis.com/auth/activity,否则您将收到错误.

where a first argument is a datetime of last check, second google drive folder id, and the last reference for client variable. Also your app should have a scope for: https://www.googleapis.com/auth/activity otherwise you will get an error.

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

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