nao-pon/flysystem-google-drive
Laravel Flysystem adapter for Google Drive. Store, read, list, update and delete files on Drive using Flysystem’s filesystem API, with support for service accounts or OAuth. Ideal for using Google Drive as a cloud disk in Laravel apps.
Install the Package
composer require nao-pon/flysystem-google-drive
Ensure league/flysystem is also installed (dependency).
Configure Google Drive Credentials
client_id, client_secret, and redirect_uri in .env:
GOOGLE_DRIVE_CLIENT_ID=your_client_id
GOOGLE_DRIVE_CLIENT_SECRET=your_client_secret
GOOGLE_DRIVE_REDIRECT_URI=http://localhost/callback
Basic Adapter Initialization
use NaoPon\FlysystemGoogleDrive\GoogleDriveAdapter;
$adapter = new GoogleDriveAdapter(
$clientId,
$clientSecret,
$redirectUri,
$accessToken // Optional: Pre-authenticated token
);
$filesystem = new \League\Flysystem\Filesystem($adapter);
First Use Case: Upload a File
$filesystem->write('folder/file.txt', 'Hello, Google Drive!');
GoogleDriveAdapter with a redirect URI to initiate OAuth. Handle the callback in a Laravel route:
Route::get('/callback', function () {
$adapter = new GoogleDriveAdapter(
config('services.google.client_id'),
config('services.google.client_secret'),
config('services.google.redirect_uri')
);
$authUrl = $adapter->getAuthUrl();
// Redirect user to $authUrl, then handle the token exchange.
});
$adapter = new GoogleDriveAdapter(
null, // No OAuth for service accounts
null,
null,
null,
$serviceAccountJsonPath
);
writeStream() to avoid memory issues:
$filesystem->writeStream('large-file.zip', fopen('local-file.zip', 'r'));
$filesystem->createDirectory('folder-id'); // 'folder-id' is a Google Drive folder ID
$metadata = $filesystem->metadata('file-id');
config/filesystems.php:
'disks' => [
'google' => [
'driver' => 'google',
'client_id' => env('GOOGLE_DRIVE_CLIENT_ID'),
'client_secret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
'redirect_uri' => env('GOOGLE_DRIVE_REDIRECT_URI'),
'access_token' => env('GOOGLE_DRIVE_ACCESS_TOKEN'),
],
],
Then use it like any other disk:
Storage::disk('google')->put('file.txt', 'Content');
google/apiclient for automatic handling.1AbCdEfGhIjKlMnOpQrStUvWxYz) rather than paths.debug option in the adapter constructor to log API requests:
$adapter = new GoogleDriveAdapter(..., ..., ..., true);
Google_Service_Exception for API errors.getMetadata().filesystem.storing).$local = new \League\Flysystem\Local\LocalFilesystemAdapter('/local/path');
$google = new GoogleDriveAdapter(...);
$composite = new \League\Flysystem\CompositeFilesystem([$local, $google]);
visibility Option: Set file visibility (e.g., 'visibility' => 'private') during uploads.Google_Service_Drive client:
$client = $adapter->getClient();
$permissions = $client->permissions->listPermissions('file-id');
How can I help you explore Laravel packages today?