This is my package laravel-google-drive-storage
yaza/laravel-google-drive-storage is a Laravel package for this is my package laravel-google-drive-storage.
It currently has 207 GitHub stars and 205.937 downloads on Packagist (latest version V5.0.0).
Install it with composer require yaza/laravel-google-drive-storage.
Discover more Laravel packages by yaza
or browse all Laravel packages to compare alternatives.
Last updated
This package allow to store and get data from google drive like S3 AWS in laravel
You can install the package via composer:
composer require yaza/laravel-google-drive-storage
copy to .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=
config filesystem.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'),
]
]
you can use storage driver function by laravel
example :
Storage::disk('google')->put($filename, File::get($filepath));
refrensi code opration sample code
Usage with accessToken. Could be useful if you want to use your user's token e.g upload a file in their own Drive.
// 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 Yaza\LaravelGoogleDriveStorage\Gdrive;
Gdrive::put('location/filename.png', $request->file('file'));
// or
Gdrive::put('filename.png', public_path('path/filename.png'));
use Yaza\LaravelGoogleDriveStorage\Gdrive;
$data = Gdrive::get('path/filename.png');
return response($data->file, 200)
->header('Content-Type', $data->ext);
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?
]);
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.'"');
use Yaza\LaravelGoogleDriveStorage\Gdrive;
Gdrive::delete('path/filename.png');
use Yaza\LaravelGoogleDriveStorage\Gdrive;
Gdrive::deleteDir('foldername');
use Yaza\LaravelGoogleDriveStorage\Gdrive;
Gdrive::makeDir('foldername');
use Yaza\LaravelGoogleDriveStorage\Gdrive;
Gdrive::renameDir('oldfolderpath', 'newfolder');
use Yaza\LaravelGoogleDriveStorage\Gdrive;
Gdrive::all('/');
// or
Gdrive::all('foldername');
output
Illuminate\Support\Collection {#804 ▼ // app/Http/Controllers/UploadController.php:70
#items: array:3 [▼
0 => League\Flysystem\DirectoryAttributes {#798 ▶}
1 => League\Flysystem\FileAttributes {#796 ▶}
2 => League\Flysystem\DirectoryAttributes {#783 ▶}
]
#escapeWhenCastingToString: false
}
use Yaza\LaravelGoogleDriveStorage\Gdrive;
Gdrive::all('/', true);
// or
Gdrive::all('foldername', true);
output
Illuminate\Support\Collection {#804 ▼ // app/Http/Controllers/UploadController.php:70
#items: array:3 [▼
0 => League\Flysystem\DirectoryAttributes {#798 ▶}
1 => League\Flysystem\FileAttributes {#796 ▶}
2 => League\Flysystem\DirectoryAttributes {#783 ▶}
]
#escapeWhenCastingToString: false
}
Using display paths as identifiers for folders and files requires them to be unique. Unfortunately Google Drive allows users to create files and folders with same (displayed) names. In such cases when unique path cannot be determined this adapter chooses the oldest (first) instance. In case the newer duplicate is a folder and user puts a unique file or folder inside the adapter will be able to reach it properly (because full path is unique).
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
Thanks to Masbug
The MIT License (MIT). Please see License File for more information.