### Install laravel-google-drive-storage via Composer Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This command installs the package using Composer. Ensure you have Composer installed and configured for your Laravel project. ```bash composer require yaza/laravel-google-drive-storage ``` -------------------------------- ### Use Laravel Storage Facade with Google Drive Disk Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This example demonstrates how to use the standard Laravel Storage facade to interact with the configured Google Drive disk for storing files. ```php Storage::disk('google')->put($filename, File::get($filepath)); ``` -------------------------------- ### Download and Display Files from Google Drive (PHP) Source: https://context7.com/yaza-putu/laravel-google-drive-storage/llms.txt Retrieve files from Google Drive using the Gdrive helper and return them as responses. The `get` method retrieves the file data, and the response is constructed with the correct content type based on the file extension. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; // Get file and display inline public function getFile($filepath) { $data = Gdrive::get($filepath); return response($data->file, 200) ->header('Content-Type', $data->ext); } // Download file with proper headers public function downloadFile($filepath) { $data = Gdrive::get('documents/report.pdf'); return response($data->file, 200) ->header('Content-Type', $data->ext) ->header('Content-Disposition', 'attachment; filename="' . $data->filename . '"'); } // Example controller method for image display public function showImage() { $imageData = Gdrive::get('uploads/profile-photo.jpg'); return response($imageData->file, 200) ->header('Content-Type', $imageData->ext) ->header('Cache-Control', 'max-age=3600'); } ``` -------------------------------- ### Get File from Google Drive using Gdrive Helper Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This retrieves a file from Google Drive using the Gdrive helper. It returns file content and its extension, suitable for direct response or further processing. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; $data = Gdrive::get('path/filename.png'); return response($data->file, 200) ->header('Content-Type', $data->ext); ``` -------------------------------- ### Get Large File with Stream from Google Drive Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This function retrieves a file from Google Drive as a stream, which is efficient for handling large files. It returns a stream resource and the file extension. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; $readStream = Gdrive::readStream('path/filename.png'); return response()->stream(function () use ($readStream) { fpassthru($readStream->file); }, 200, [ 'Content-Type' => $readStream->ext, //'Content-disposition' => 'attachment; filename="'.$filename.'"', // force download? ]); ``` -------------------------------- ### Stream Large Files from Google Drive Source: https://context7.com/yaza-putu/laravel-google-drive-storage/llms.txt Demonstrates how to stream large files from Google Drive to prevent memory exhaustion. It utilizes `Gdrive::readStream` to get a file stream and `response()->stream` to send it to the client. This method is ideal for video or large backup files and supports download options. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; // Stream large video file public function streamVideo($filepath) { $readStream = Gdrive::readStream('videos/presentation.mp4'); return response()->stream(function () use ($readStream) { fpassthru($readStream->file); }, 200, [ 'Content-Type' => $readStream->ext, 'Accept-Ranges' => 'bytes', ]); } // Stream file with download option public function streamDownload($filepath) { $readStream = Gdrive::readStream($filepath); return response()->stream(function () use ($readStream) { fpassthru($readStream->file); }, 200, [ 'Content-Type' => $readStream->ext, 'Content-Disposition' => 'attachment; filename="' . $readStream->filename . '"', ]); } // Stream large backup file public function downloadBackup() { $stream = Gdrive::readStream('backups/database-backup.sql'); return response()->stream(function () use ($stream) { fpassthru($stream->file); }, 200, [ 'Content-Type' => 'application/sql', 'Content-Disposition' => 'attachment; filename="backup.sql"', ]); } ``` -------------------------------- ### Get Google Drive File Information (PHP) Source: https://context7.com/yaza-putu/laravel-google-drive-storage/llms.txt Extract metadata and information from Google Drive file paths using the Gdrive facade. This function retrieves the filename, extension, and full path of a given file. It also includes a validation step to check if the file extension is among the allowed types before processing. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; // Get file information public function getFileInfo($filepath) { $info = Gdrive::getFileInfo('documents/reports/annual-report.pdf'); return response()->json([ 'filename' => $info->filename, 'extension' => $info->ext, 'full_path' => $info->path ]); // Output example: // { // "filename": "annual-report.pdf", // "extension": "pdf", // "full_path": "documents/reports/annual-report.pdf" // } } // Validate file before processing public function processFile($filepath) { $info = Gdrive::getFileInfo($filepath); $allowedExtensions = ['jpg', 'png', 'pdf', 'docx']; if (!in_array($info->ext, $allowedExtensions)) { return response()->json([ 'error' => 'Invalid file type', 'provided' => $info->ext, 'allowed' => $allowedExtensions ], 400); } // Process file... $data = Gdrive::get($filepath); return response()->json([ 'message' => 'File processed', 'filename' => $info->filename ]); } ``` -------------------------------- ### Configure Dynamic Google Drive Disks (PHP) Source: https://context7.com/yaza-putu/laravel-google-drive-storage/llms.txt Build on-demand Google Drive disks with user-specific access tokens using Laravel's Storage facade. This allows for user-specific uploads and synchronization to multiple drives by providing different access and refresh tokens. Configuration requires client ID, client secret, and the user's access token. ```php use Illuminate\Support\Facades\Storage; use Illuminate\Http\Request; use Illuminate\Support\Facades\File; // Use authenticated user's access token public function uploadToUserDrive(Request $request) { // Build an on-demand disk with user's credentials $disk = Storage::build([ 'driver' => 'google', 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), 'accessToken' => auth()->user()->google_access_token, 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), 'folder' => env('GOOGLE_DRIVE_FOLDER'), ]); $file = $request->file('document'); $filename = 'user_uploads/' . time() . '_' . $file->getClientOriginalName(); $disk->put($filename, File::get($file)); return response()->json([ 'message' => 'Uploaded to user\'s Google Drive', 'filename' => $filename ]); } // Multiple user drives handling public function syncToMultipleDrives($filePath, $users) { foreach ($users as $user) { $disk = Storage::build([ 'driver' => 'google', 'clientId' => config('services.google.client_id'), 'clientSecret' => config('services.google.client_secret'), 'accessToken' => $user->google_access_token, 'refreshToken' => $user->google_refresh_token, 'folder' => 'shared_documents', ]); $disk->put(basename($filePath), file_get_contents($filePath)); } return response()->json(['message' => 'Synced to ' . count($users) . ' drives']); } ``` -------------------------------- ### Configure Filesystem Disk for Google Drive (PHP) Source: https://context7.com/yaza-putu/laravel-google-drive-storage/llms.txt Add a new disk configuration in `config/filesystems.php` to define the 'google' driver. This specifies the driver type and maps environment variables for authentication and folder settings. ```php // config/filesystems.php - Add disk configuration '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'), ], // Add other disks... ] ``` -------------------------------- ### Build On-Demand Google Drive Disk with User Access Token Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This code shows how to dynamically build a Google Drive disk instance using an authenticated user's access token, useful for user-specific uploads. ```php $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)); ``` -------------------------------- ### Manage Directories on Google Drive Source: https://context7.com/yaza-putu/laravel-google-drive-storage/llms.txt Illustrates how to manage directory structures on Google Drive using `Gdrive::makeDir` and `Gdrive::renameDir`. This includes creating new directories, setting up nested directory structures for users, renaming existing directories, and organizing files by year. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; // Create new directory public function createFolder(Request $request) { $folderName = $request->input('folder_name'); Gdrive::makeDir($folderName); return response()->json([ 'message' => 'Folder created', 'folder' => $folderName ]); } // Create nested directory structure public function setupUserStorage($userId) { Gdrive::makeDir("users/{$userId}"); Gdrive::makeDir("users/{$userId}/documents"); Gdrive::makeDir("users/{$userId}/images"); return response()->json(['message' => 'User storage structure created']); } // Rename directory public function renameFolder(Request $request) { $oldPath = 'projects/old-project'; $newPath = 'projects/new-project'; Gdrive::renameDir($oldPath, $newPath); return response()->json(['message' => 'Folder renamed successfully']); } // Organize files by year public function organizeByYear() { Gdrive::makeDir('archives/2024'); Gdrive::renameDir('uploads/current', 'archives/2024/uploads'); return response()->json(['message' => 'Files organized']); } ``` -------------------------------- ### Upload Files using Gdrive Helper (PHP) Source: https://context7.com/yaza-putu/laravel-google-drive-storage/llms.txt Upload files to Google Drive using the Gdrive helper class. Supports uploading from an uploaded file in a request or from a local file path. The `put` method takes the destination path and the file content or path. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; use Illuminate\Support\Facades\Storage; // Using Gdrive helper with uploaded file public function uploadFromRequest(Request $request) { // Upload from request file Gdrive::put('documents/report.pdf', $request->file('document')); return response()->json(['message' => 'File uploaded successfully']); } // Using Gdrive helper with local file path public function uploadFromPublic() { Gdrive::put('images/logo.png', public_path('assets/logo.png')); return response()->json(['message' => 'Logo uploaded to Google Drive']); } ``` -------------------------------- ### List Google Drive Files and Directories (PHP) Source: https://context7.com/yaza-putu/laravel-google-drive-storage/llms.txt Retrieve and display contents of Google Drive folders using the Gdrive facade. Supports listing root directory, specific folders (without recursion), all files recursively, and searching for specific file types like PDFs. It leverages LeagueFlysystem adapters for file and directory attributes. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; // List root directory contents public function listRoot() { $contents = Gdrive::all('/'); return response()->json([ 'total' => $contents->count(), 'items' => $contents ]); } // List specific folder without recursion public function listFolder($folderpath) { $contents = Gdrive::all('documents', false); $files = $contents->filter(function ($item) { return $item instanceof \League\Flysystem\FileAttributes; }); $folders = $contents->filter(function ($item) { return $item instanceof \League\Flysystem\DirectoryAttributes; }); return response()->json([ 'files' => $files->count(), 'folders' => $folders->count(), 'contents' => $contents ]); } // List all files recursively including subdirectories public function listAllFiles() { $allContents = Gdrive::all('/', true); $fileList = $allContents->map(function ($item) { if ($item instanceof \League\Flysystem\FileAttributes) { return [ 'path' => $item->path(), 'size' => $item->fileSize(), 'timestamp' => $item->lastModified(), ]; } })->filter(); return response()->json(['files' => $fileList]); } // Search for specific file types public function listPdfDocuments() { $contents = Gdrive::all('documents', true); $pdfs = $contents->filter(function ($item) { return $item instanceof \League\Flysystem\FileAttributes && str_ends_with($item->path(), '.pdf'); }); return response()->json(['pdf_documents' => $pdfs]); } ``` -------------------------------- ### Upload Files using Laravel Storage Facade (PHP) Source: https://context7.com/yaza-putu/laravel-google-drive-storage/llms.txt Upload files to Google Drive using Laravel's Storage facade with the 'google' disk. This method allows you to leverage the standard Laravel Storage API for file uploads, providing the filename and file content. ```php use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\File; // Assuming File facade is used for getting file content // Using Laravel Storage facade public function uploadWithStorage(Request $request) { $file = $request->file('file'); $filename = time() . '_' . $file->getClientOriginalName(); Storage::disk('google')->put($filename, File::get($file)); return response()->json([ 'message' => 'Uploaded successfully', 'filename' => $filename ]); } ``` -------------------------------- ### Make Directory on Google Drive using Gdrive Helper Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This function creates a new directory on Google Drive at the specified path. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::makeDir('foldername'); ``` -------------------------------- ### Configure .env for Google Drive Storage Source: https://context7.com/yaza-putu/laravel-google-drive-storage/llms.txt Set environment variables in your .env file to configure authentication and storage details for Google Drive. This includes client ID, client secret, refresh token, and optional access token and folder path. ```dotenv FILESYSTEM_CLOUD=google GOOGLE_DRIVE_CLIENT_ID=your-client-id.apps.googleusercontent.com GOOGLE_DRIVE_CLIENT_SECRET=your-client-secret GOOGLE_DRIVE_REFRESH_TOKEN=your-refresh-token GOOGLE_DRIVE_ACCESS_TOKEN=your-access-token GOOGLE_DRIVE_FOLDER= ``` -------------------------------- ### Configure Google Drive Environment Variables Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md These environment variables are required to authenticate and configure the Google Drive connection. Replace 'xxx' with your actual Google API credentials and token. ```env 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= ``` -------------------------------- ### Download File from Google Drive using Gdrive Helper Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This method retrieves a file from Google Drive and returns it as a downloadable attachment with the correct content type and filename. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; $data = Gdrive::get('path/filename.png'); return response($data->file, 200) ->header('Content-Type', $data->ext) ->header('Content-disposition', 'attachment; filename="'.$data->filename.'"'); ``` -------------------------------- ### List All Folders and Files in a Directory Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This method retrieves a collection of all files and directories within a specified Google Drive path. It returns an Illuminate\Support\Collection object. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::all('/'); // or Gdrive::all('foldername'); ``` -------------------------------- ### Configure Filesystem Disk for Google Drive Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This configuration snippet adds a 'google' disk to your Laravel filesystem configuration, allowing you to use the Google Drive driver for storage operations. ```php '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'), ] ] ``` -------------------------------- ### Put File to Google Drive using Gdrive Helper Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This method uses the Gdrive helper class provided by the package to upload files to Google Drive. It supports uploading from a request file or a local path. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::put('location/filename.png', $request->file('file')); // or Gdrive::put('filename.png', public_path('path/filename.png')); ``` -------------------------------- ### List All Folders and Files Recursively Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This method retrieves a collection of all files and directories within a specified Google Drive path, including those in subdirectories. The second argument `true` enables recursive listing. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::all('/', true); // or Gdrive::all('foldername', true); ``` -------------------------------- ### Delete Files and Directories on Google Drive Source: https://context7.com/yaza-putu/laravel-google-drive-storage/llms.txt Provides methods for removing files and directories from Google Drive storage using the `Gdrive::delete` and `Gdrive::deleteDir` functions. It covers deleting single files, multiple files, and entire directories, including cleanup operations for old uploads. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; // Delete a single file public function deleteFile($filepath) { Gdrive::delete('documents/old-report.pdf'); return response()->json(['message' => 'File deleted successfully']); } // Delete multiple files public function deleteMultiple(Request $request) { $files = $request->input('files'); // ['file1.pdf', 'file2.jpg', ...] foreach ($files as $file) { Gdrive::delete($file); } return response()->json(['message' => count($files) . ' files deleted']); } // Delete entire directory public function deleteFolder($foldername) { Gdrive::deleteDir('uploads/temp-folder'); return response()->json(['message' => 'Folder deleted successfully']); } // Clean up old uploads public function cleanupOldUploads() { Gdrive::deleteDir('uploads/2023'); return response()->json(['message' => 'Old uploads cleaned']); } ``` -------------------------------- ### Rename Directory on Google Drive using Gdrive Helper Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This command renames an existing directory on Google Drive from an old path to a new name. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::renameDir('oldfolderpath', 'newfolder'); ``` -------------------------------- ### Delete Directory from Google Drive using Gdrive Helper Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This command deletes a specified directory and its contents from Google Drive. Use with caution as it's a recursive deletion. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::deleteDir('foldername'); ``` -------------------------------- ### Delete File from Google Drive using Gdrive Helper Source: https://github.com/yaza-putu/laravel-google-drive-storage/blob/main/README.md This command deletes a specified file from Google Drive using its path. ```php use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::delete('path/filename.png'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.