laravel-google-drive-storage

repository·main·Indexed 41 days ago

https://github.com/yaza-putu/laravel-google-drive-storage

A 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.

Tokens
3.3K
Snippets
19
Records
20
Agent score
52%

What's inside laravel-google-drive-storage

  1. Understand Google Drive path limitations

    main
    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.
  2. Configure the Google Drive filesystem disk

    main

    Register the google driver in your config/filesystems.php file. This allows you to use the standard Laravel Storage facade 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'),
        ]
    ]
  3. Configure Google Drive environment variables

    main

    Add the following keys to your .env file to provide the necessary Google API credentials. The GOOGLE_DRIVE_FOLDER option 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=
  4. Build an on-demand Google Drive disk for specific users

    main

    If 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));
  5. Use the Gdrive helper class for file operations

    main

    The package provides a Yaza\LaravelGoogleDriveStorage\Gdrive helper 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');
  6. List files and folders with Gdrive::all()

    main

    Use 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 an Illuminate\Support\Collection containing League\Flysystem\DirectoryAttributes and League\Flysystem\FileAttributes objects.

    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);
  7. Configure the 'google' storage driver

    main

    The package extends Laravel's Storage facade by adding a google driver. 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
    ],
  8. Access the Google Drive storage service via the LaravelGoogleDriveStorage facade

    main

    The LaravelGoogleDriveStorage facade provides a static proxy to the underlying Yaza\LaravelGoogleDriveStorage\LaravelGoogleDriveStorage service. 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();
  9. Get file metadata with `Gdrive::getFileInfo()`

    main

    Use Gdrive::getFileInfo($file_path) to retrieve metadata about a file without downloading its content. It returns a GdriveFileInfo object 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'
  10. Delete files and directories in Google Drive

    main

    You 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/');