How to check if content of google drive folder has changed?

本小妞迷上赌 提交于 2019-12-06 14:58:54

问题


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.


回答1:


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;
    }

}

which can be used like:

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

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.



来源:https://stackoverflow.com/questions/53470637/how-to-check-if-content-of-google-drive-folder-has-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!