Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Snapey's avatar
Level 122

Google Drive query examples

Does anyone have any examples of how to use Google Drive query parameters with flysystem?

In particular, accessing a folder 'Shared With Me'

0 likes
2 replies
LaryAI's avatar
Level 58

Here's an example of how to use Google Drive query parameters with Flysystem:

use League\Flysystem\Filesystem;
use Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter;

$client = new \Google_Client();
$client->setClientId('your_client_id');
$client->setClientSecret('your_client_secret');
$client->setRedirectUri('your_redirect_uri');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');

$service = new \Google_Service_Drive($client);

$adapter = new GoogleDriveAdapter($service, 'root');
$filesystem = new Filesystem($adapter);

// Accessing a folder 'Shared With Me'
$sharedWithMeFolderId = 'root';
$parameters = [
    'q' => 'sharedWithMe=true and mimeType="application/vnd.google-apps.folder"',
];
$files = $service->files->listFiles($parameters);
foreach ($files->getFiles() as $file) {
    if ($file->getName() === 'Shared With Me') {
        $sharedWithMeFolderId = $file->getId();
        break;
    }
}

$adapter = new GoogleDriveAdapter($service, $sharedWithMeFolderId);
$filesystem = new Filesystem($adapter);

This code sets up a Google Drive client and service, creates a Flysystem adapter using the Google Drive adapter, and then uses the Google Drive API to find the ID of the 'Shared With Me' folder. Finally, it creates a new adapter and filesystem using the ID of the 'Shared With Me' folder.

Note that you'll need to replace 'your_client_id', 'your_client_secret', and 'your_redirect_uri' with your own values. You'll also need to install the 'hypweb/flysystem-google-drive' package via Composer.

Snapey's avatar
Level 122

LaryAi answer helped a little. From various sources I settled on the approach below.

Some of this could have been abstracted to a service provider class, but I don't need this code other than in this one class;

private function getRecentFiles(System $system)
{
    $client = new \Google_Client();
    $client->setClientId(config('filesystems.disks.google.clientId'));
    $client->setClientSecret(config('filesystems.disks.google.clientSecret'));
    $client->refreshToken(config('filesystems.disks.google.refreshToken'));
    
    $this->service = new Google_Service_Drive($client);

    $since = today()
        ->tz('Australia/Sydney')
        ->subdays(1)
        ->toIso8601String();

    $parameters = [
        "includeTeamDriveItems"=>true,   // files are in a folder shared with me
        "supportsTeamDrives"=>true,
        "q"=>"createdTime>'{$since}'"
    ];
    
    $files = collect($this->service->files->listFiles($parameters)->files);

    $files = $files->reject(function($file) {
        return $file->mimeType != 'text/csv';
    });

    return $files;
}

The parameters array was key to my success. Quoting the ISO date within the query was unexpected but required.

What really helped was this https://developers.google.com/drive/api/v3/reference/files/list and the "Try This Method" panel. If you expand this out, it shows the API parameters based on the selection and the response.

I used collection's reject method to ensure my file list only text/csv files which is what I needed to process in this case.

1 like

Please or to participate in this conversation.