How to check if content of google drive folder has

2019-04-17 07:37发布

问题:

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.