laravel-google-drive-storage
repository·main·Indexed 41 days ago
https://github.com/yaza-putu/laravel-google-drive-storageA Laravel package that enables Google Drive as a filesystem storage driver. It allows developers to use the standard Laravel Storage facade with a 'google' disk or utilize the Gdrive helper class for advanced file and directory management, including on-demand disk building for specific users via access tokens.
What's inside laravel-google-drive-storage
- This adapter uses display paths as identifiers. Because Google Drive allows multiple files or folders to have the same display name in the same location, the adapter may encounter ambiguity. In such cases, the adapter selects the oldest (first) instance found. However, if a unique file or folder is placed inside a duplicate folder, the adapter can still reach it because the full path becomes unique.
Install laravel-google-drive-storage via Composer
mainInstall the package using composer to enable Google Drive storage as a filesystem driver in your Laravel application.
composer require yaza/laravel-google-drive-storageConfigure the Google Drive filesystem disk
mainRegister the
googledriver in yourconfig/filesystems.phpfile. This allows you to use the standard LaravelStoragefacade with the Google Drive driver.'disks' => [ 'google' => [ 'driver' => 'google', 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), 'accessToken' => env('GOOGLE_DRIVE_ACCESS_TOKEN'), // optional 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), 'folder' => env('GOOGLE_DRIVE_FOLDER'), ] ]Configure Google Drive environment variables
mainAdd the following keys to your
.envfile to provide the necessary Google API credentials. TheGOOGLE_DRIVE_FOLDERoption is used to define a base directory for your files.FILESYSTEM_CLOUD=google GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com GOOGLE_DRIVE_CLIENT_SECRET=xxx GOOGLE_DRIVE_REFRESH_TOKEN=xxx GOOGLE_DRIVE_ACCESS_TOKEN=xxx GOOGLE_DRIVE_FOLDER=Use the Google Drive disk via Laravel Storage facade
mainOnce configured, you can interact with Google Drive using the standard Laravel
Storagefacade by specifying thegoogledisk.Storage::disk('google')->put($filename, File::get($filepath));Build an on-demand Google Drive disk for specific users
mainIf you need to upload files to a specific user's Google Drive, you can build a disk on-the-fly using their unique
accessToken.// Build an on-demand disk $disk = Storage::build([ 'driver' => 'google', 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), 'accessToken' => auth()->user()->google_access_token, // Get from authenticated user 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), 'folder' => env('GOOGLE_DRIVE_FOLDER'), ]); $disk->put($filename, File::get($filepath));Use the Gdrive helper class for file operations
mainThe package provides a
Yaza\LaravelGoogleDriveStorage\Gdrivehelper class for simplified file and directory management.use Yaza\ LaravelGoogleDriveStorage\ Gdrive; // Put File Gdrive::put('location/filename.png', $request->file('file')); // or Gdrive::put('filename.png', public_path('path/filename.png')); // Get File $data = Gdrive::get('path/filename.png'); // Get Large File with stream $readStream = Gdrive::readStream('path/filename.png'); // Delete file Gdrive::delete('path/filename.png'); // Delete directory Gdrive::deleteDir('foldername'); // Make directory Gdrive::makeDir('foldername'); // Rename directory Gdrive::renameDir('oldfolderpath', 'newfolder');List files and folders with Gdrive::all()
mainUse
Gdrive::all()to retrieve a collection of files and directories. You can pass a path and an optional boolean to include subfolders (recursive search). The output is anIlluminate\Support\CollectioncontainingLeague\Flysystem\DirectoryAttributesandLeague\Flysystem\FileAttributesobjects.use Yaza\LaravelGoogleDriveStorage\Gdrive; // All files and folders in a directory Gdrive::all('/'); // All files and folders in a directory (recursive) Gdrive::all('foldername', true);Configure the 'google' storage driver
mainThe package extends Laravel's
Storagefacade by adding agoogledriver. To use it, you must provide a configuration array containing Google API credentials and folder settings.Supported configuration keys:
clientId: Your Google Client ID.clientSecret: Your Google Client Secret.refreshToken: Your Google Refresh Token.accessToken(optional): An existing access token to use instead of refreshing.folder(optional): The specific Google Drive folder to use as the root. Defaults to/.teamDriveId(optional): The ID of the Google Team Drive to target.
// Example configuration structure for the 'google' driver 'google' => [ 'clientId' => 'YOUR_CLIENT_ID', 'clientSecret' => 'YOUR_CLIENT_SECRET', 'refreshToken' => 'YOUR_REFRESH_TOKEN', 'accessToken' => null, // optional 'folder' => '/path/to/folder', // optional 'teamDriveId' => null, // optional ],Access the Google Drive storage service via the LaravelGoogleDriveStorage facade
mainThe
LaravelGoogleDriveStoragefacade provides a static proxy to the underlyingYaza\LaravelGoogleDriveStorage\LaravelGoogleDriveStorageservice. Use this facade to interact with Google Drive storage within your Laravel application without manually resolving the service from the container.use Yaza\LaravelGoogleDriveStorage\Facades\LaravelGoogleDriveStorage; // Example usage (actual methods depend on the underlying service implementation) LaravelGoogleDriveStorage::someMethod();Get file metadata with `Gdrive::getFileInfo()`
mainUse
Gdrive::getFileInfo($file_path)to retrieve metadata about a file without downloading its content. It returns aGdriveFileInfoobject containing:filename: The name of the file.ext: The file extension.path: The normalized path (using forward slashes).
use Yaza\LaravelGoogleDriveStorage\Gdrive; $info = Gdrive::getFileInfo('folder/subfolder/document.pdf'); echo $info->filename; // 'document.pdf' echo $info->ext; // 'pdf' echo $info->path; // 'folder/subfolder/document.pdf'Delete files and directories in Google Drive
mainYou can remove individual files or entire directories using the following methods:
Gdrive::delete($path): Deletes a specific file at the provided path.Gdrive::deleteDir($dirpath): Deletes an entire directory and its contents.
use Yaza\LaravelGoogleDriveStorage\Gdrive; // Delete a file Gdrive::delete('documents/old_file.txt'); // Delete a directory Gdrive::deleteDir('temp_folder/');