Setup A Laravel Storage Driver With Google Drive API GitHub
Setup A Laravel Storage Driver With Google Drive API GitHub
sergomet / GoogleDriveServiceProvider.php
Forked from ivanvermeyen/!NOTE.md
Created 5 years ago
Star
Code
Revisions
7
Stars
66
Forks
25
README.md
https://console.developers.google.com/
After you enter a name, it takes a few seconds before the project is successfully created on
the server.
Then go to Library and click on "Drive API" under "Google Apps APIs".
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 1/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
Then, go to "Credentials" and click on the tab "OAuth Consent Screen". Fill in a "Product
name shown to users" and Save it. Don't worry about the other fields.
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 2/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
Then go back to Credentials, click the button that says "Create Credentials" and select
"OAuth Client ID".
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 3/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
Enter your "Authorized redirect URIs", preferably your test URL (http://mysite.dev) and your
production URL (https://mysite.com) - or create a separate production key later. Also add
https://developers.google.com/oauthplayground temporarily, because you will need to use
that in the next step.
Click Create and take note of your Client ID and Client Secret.
Make sure you added this URL to your Authorized redirect URIs in the previous step.
In the top right corner, click the settings icon, check "Use your own OAuth credentials" and
paste your Client ID and Client Secret.
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 4/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
In step 1 on the left, scroll to "Drive API v3", expand it and check each of the scopes.
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 5/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
Click "Authorize APIs" and allow access to your account when prompted.
When you get to step 2, check "Auto-refresh the token before it expires" and click
"Exchange authorization code for tokens".
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 6/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
When you get to step 3, click on step 2 again and you should see your refresh token.
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 7/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
Because Google Drive allows for duplicate names, it identifies each file and folder with a
unique ID.
If you open your folder, you will see the Folder ID in the URL.
Install in Laravel
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 8/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
return [
// ...
'cloud' => 'google', // Optional: set Google Drive as default cloud storage
'disks' => [
// ...
'google' => [
'driver' => 'google',
],
// ...
],
// ...
];
GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=xxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxx
GOOGLE_DRIVE_FOLDER_ID=null
App\Providers\GoogleDriveServiceProvider::class,
Test Drive
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 9/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
Route::get('test', function() {
});
GoogleDriveServiceProvider.php
1 <?php
2
3 namespace App\Providers;
4
5 use Illuminate\Support\ServiceProvider;
6
7 class GoogleDriveServiceProvider extends ServiceProvider
8 {
9 /**
10 * Bootstrap the application services.
11 *
12 * @return void
13 */
14 public function boot()
15 {
16 \Storage::extend('google', function($app, $config) {
17 $client = new \Google_Client();
18 $client->setClientId($config['clientId']);
19 $client->setClientSecret($config['clientSecret']);
20 $client->refreshToken($config['refreshToken']);
21 $service = new \Google_Service_Drive($client);
22 $adapter = new \Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter($service, $config['fol
23
24 return new \League\Flysystem\Filesystem($adapter);
25 });
26 }
27
28 /**
29 * Register the application services.
30 *
31 * @return void
32 */
33 public function register()
34 {
35 //
36 }
37 }
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 10/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
Al-Kaiser
commented on Aug 6, 2018
idcProger
commented on Aug 8, 2018
{\n
"error": {\n
"errors": [\n
{\n
"domain": "global",\n
"reason": "required",\n
"locationType": "header",\n
"location": "Authorization"\n
}\n
],\n
"code": 401,\n
}\n
}\n
cgmalaquias
commented on Apr 10, 2019
The same:
{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType":
"header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } }
KyryloGrygorenko
commented on Apr 25, 2019
•
edited
Please Help! { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required",
"locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } }
ThuyPhan97
commented on Jun 17, 2019
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 11/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
I also got this error but i followed the correct instructions. it worked for me
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 12/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
afrasiyabhaider
commented on Oct 20, 2019
"error": {
"errors": [
"domain": "global",
"reason": "insufficientPermissions",
],
"code": 403,
"
sergomet
commented on Oct 22, 2019 Author
sammak1432
commented on Nov 18, 2019
•
edited
"error": {
"errors": [
"domain": "global",
"reason": "required",
"locationType": "header",
"location": "Authorization"
],
"code": 401,
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 13/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
ponikar
commented on Dec 14, 2019
JuanAdamuz
commented on Dec 19, 2019
I have the same problem. Nor able to get the file by its name
BibekStha123
commented on Dec 31, 2019
thank you so much. How to get the folders from the drive?
afrasiyabhaider
commented on Jan 4, 2020
thank you so much. How to get the folders from the drive?
Storage::disk('google_drive')->url($item['path'])
By writing this piece of code, you can get a downloadable link of your file
matteosantini
commented on Apr 1, 2020
For all guys who have the "Login Required" issue maybe It's because you don't pass the access token in the
right way.
I had the same problem and I fixed it with the fetchAccessTokenWithRefreshToken function, try with this:
$client->setAccessType('offline');
$client->setClientId($config['clientId']);
$client->refreshToken($config['refreshToken']);
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 14/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
ericwang401
commented on May 31, 2020
Works well, but I have a problem is that it returns random strings for the names.
["1ci2uGHETqRSAsojJKFXIqrVSrxLs4xgv","1Ja081Rq8nf-2LlL-UsJA0k49v4Ek47_D"]
I'm not sure if there is a special parameter I missed, but I couldn't find a similar occurrence of randomized file
names.
ponikar
commented on May 31, 2020
•
edited
Use core php google drive drivers. I had the same issue. It turns out I had to use core google drive
configuration rather than stoage facade.
avidianity
commented on Jun 2, 2020
{↵ "error": {↵ "errors": [↵ {↵ "domain": "global",↵ "reason": "notFound",↵ "message": "File not found: image.",↵
"locationType": "parameter",↵ "location": "fileId"↵
}↵
}↵
}↵"
My code is
// Illuminate\Http\UploadedFile
$file->store('files')
ponikar
commented on Jun 3, 2020
It seems the file is missing! Please prefer google drive api documentation! Or the code is mentioned above.
Try that one
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 15/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
mh19
commented on Jul 9, 2020
Works well, but I have a problem is that it returns random strings for the names.
["1ci2uGHETqRSAsojJKFXIqrVSrxLs4xgv","1Ja081Rq8nf-2LlL-UsJA0k49v4Ek47_D"]
I'm not sure if there is a special parameter I missed, but I couldn't find a similar occurrence of
randomized file names.
santoshupadhyay
commented on Jul 27, 2020
•
edited
Hi folks i tried the above steps and it is being configured properly but when trying to run the route it returns
disk googl doesn't have configured driver. Although have configured same.
ponikar
commented on Jul 29, 2020
Helllo Santosh! I would suggest you to go for core PHP google drive library! It will be easier!
https://developers.google.com/drive/api/v3/quickstart/php
omardevm
commented on Aug 31, 2020
ponikar
commented on Aug 31, 2020
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 16/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
Did you pass configure your project with OAuth credentials? Please visit the link I have mentioned
satriokusuma
commented on Oct 26, 2020
Sory guys iam just ask how to show img in blade after upload in goggle drive thanks
satriokusuma
commented on Oct 26, 2020
if ($this->image) {
return Storage::disk('google')->url('1RJ_GfF69LlD-H51Tjj4YW_hfLDzlylnk'.$this->image);
// return $image;
} else {
return asset('img/no-image-default.png');
Mesbahema
commented on Nov 2, 2020
•
edited
if ($this->image) {
return Storage::disk('google')->url('1RJ_GfF69LlD-H51Tjj4YW_hfLDzlylnk'.$this->image);
// return $image;
} else {
return asset('img/no-image-default.png');
I had the same problem, but there is no need to use asset function.
1.You should first get the path of the file by the following code, it will returns you an array,Which you can find
the path from:
Storage::cloud()->listContents(<folder_path>);
or
Storage::disk('google')->listContents(<folder_path>);
2.Then you should create a route that returns the file as follows:
Route::get(<your_path>, function(){
return Storage::cloud()->response(<file_path>);
});
jazz7381
commented on Nov 17, 2020
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 17/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
riosantv
commented on Nov 27, 2020
by running
riosantv
commented on Nov 27, 2020
mahmudtopu3
commented on Dec 1, 2020
{↵ "error": {↵ "errors": [↵ {↵ "domain": "global",↵ "reason": "notFound",↵ "message": "File not found:
image.",↵ "locationType": "parameter",↵ "location": "fileId"↵
}↵
}↵
}↵"
My code is
// Illuminate\Http\UploadedFile
$file->store('files')
This happens due to special character on the string which is by default invalid in .env file, you can directly put
the credentials at config/filesystems.php
riosantv
commented on Dec 9, 2020
{↵ "error": {↵ "errors": [↵ {↵ "domain": "global",↵ "reason": "notFound",↵ "message": "File not found:
image.",↵ "locationType": "parameter",↵ "location": "fileId"↵
}↵
}↵
}↵"
My code is
// Illuminate\Http\UploadedFile
$file->store('files')
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 18/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
This happens due to special character on the string which is by default invalid in .env file, you can
directly put the credentials at config/filesystems.php
Storage::disk('gdrive')->delete(file_id);
Where gdrive is the driver name and file_id is the path of google drive's file
netwons
commented on Dec 13, 2020
•
edited
this is mycode
Route::post('/upload',function(\Illuminate\Http\Request $request){
dd( $request->file('thing')->store('google'));
});
Jangbe
commented on Dec 14, 2020
riosantv
commented on Dec 14, 2020
Can i see your .env? did you attach the folder id? Pada tanggal Min, 13 Des 2020 21.54, netwons
[email protected] menulis:
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 19/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
Visit this git and you will get all your answers. https://github.com/ivanvermeyen/laravel-google-drive-demo
ponikar
commented on Dec 14, 2020
I wanna know if Google Drive API is faster in terms of performance? I was working with this API before turns
out it was taking a long time to upload images on Drive!
netwons
commented on Dec 14, 2020
•
edited
@Jangbe
Jangbe
commented on Dec 14, 2020
•
edited
$content = $request->file('name')->getContent();
Storage::disk(google)->put('example.jpg', $content)
netwons
commented on Dec 15, 2020
•
edited
@Jangbe
okey
netwons
commented on Dec 16, 2020
•
edited
@Jangbe
Jangbe
commented on Dec 17, 2020
@Jangbe
amirkhan402
commented on Feb 6, 2021
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 20/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
Google\Service\Exception
{ "error":
{ "errors": [ {
"domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location":
"Authorization" } ],
"code": 401,
Facing this issue, anyone facing the similar issue? how can I get rid of this thing
tarzanking
commented on Feb 7, 2021
Hi, I have successfully integrated with the API but each time upload it to create a new file with the same name
cant be overwritten. Anyway to overwrite the existing file without deleting it?
`Storage::disk('public')->put('products.json', json_encode($response));`
wgerro
commented on Feb 20, 2021
Roar1827
commented on Mar 11, 2021
Anyone getting the 401 login required error: double check that when you copy/pasted your client ID that it
didn't add a trailing slash on you.
myazidinniam
commented on Mar 18, 2021
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 21/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
{\n
"error": {\n
"errors": [\n
{\n
"domain": "global",\n
"reason": "required",\n
"location": "Authorization"\n
}\n
],\n
"code": 401,\n
}\n
}\n
you can change env file on section GOOGLE_REFRESH_TOKEN add delimiter double quote for value inside
GOOGLE_REFRESH_TOKEN="1//04xd08cKBcYDWCgYIARAAGAQSNwF-
L9IruTrG9cNMQajASodNAnuoYRPsPWpbJ9QFwzBPP_ny9nB8_xxxxxxxxxx"
myazidinniam
commented on Mar 18, 2021
Google\Service\Exception
{ "error":
{ "errors": [ {
"code": 401,
Facing this issue, anyone facing the similar issue? how can I get rid of this thing
you can change env file on section GOOGLE_REFRESH_TOKEN add delimiter double quote for value inside
GOOGLE_REFRESH_TOKEN="1//04xd08cKBcYDWCgYIARAAGAQSNwF-
L9IruTrG9cNMQajASodNAnuoYRPsPWpbJ9QFwzBPP_ny9nB8_xxxxxxxxxx"
paulcedo
commented on Apr 14, 2021
•
edited
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 22/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
Hi, may i know why our refresh token expired every week? any suggestion? we already checked the Auto
refresh before it expires
swamyyadav
commented on Apr 16, 2021
refresh token expired every week, even after selecting the "Auto-refresh the token before it expires" check
box, Kindly help on this.
THANKS IN ADVANCE.
riosantv
commented on Apr 16, 2021
refresh token expired every week, even after selecting the "Auto-refresh the token before it expires"
check box, Kindly help on this.
THANKS IN ADVANCE.
Follow the tutorial step step by step given above. Me too using the same and never got any token errors.
SendraIlhamMan…
commented on Apr 21, 2021
if ($this->image) {
return Storage::disk('google')->url('1RJ_GfF69LlD-H51Tjj4YW_hfLDzlylnk'.$this->image);
// return $image;
} else {
return asset('img/no-image-default.png');
I had the same problem, but there is no need to use asset function.
1.You should first get the path of the file by the following code, it will returns you an array,Which you
can find the path from:
Storage::cloud()->listContents(<folder_path>);
or
Storage::disk('google')->listContents(<folder_path>);
2.Then you should create a route that returns the file as follows:
Route::get(<your_path>, function(){
return Storage::cloud()->response(<file_path>);
});
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 23/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
nurpuji97
commented on Jun 17, 2021
Error Otorisasi
The developer hasn’t given you access to this app. It’s currently being tested and it hasn’t been verified by
Google. If you think you should have access
erwinpratama
commented on Jun 20, 2021
faizankhalil007
commented on Jun 23, 2021
Hi, Is there anyone facing the problem of limited files? I'm getting only 500 records although I have more
than 45,000 files in my google drive directory?
afagard
commented on Jun 29, 2021
Hi, Is there anyone facing the problem of limited files? I'm getting only 500 records although I have
more than 45,000 files in my google drive directory?
I believe it is due to Google Drive API limitations, you can only have so many results in a single request. This
is pretty standard across all of Google's API's. I'm not sure it is something you can easily override.
That being said, if you have 45k files, chances are your server would run out of memory before being able to
do anything with the array of data. Suggest processing in chunks.
swamyyadav
commented on Jul 14, 2021
$content = $request->file('name')->getContent();
Storage::disk(google)->put('example.jpg', $content)
Hi, This not working for me[ got bad method], i tried differently for laravel 5.2
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 24/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
valiantboymaksud
commented on Oct 1, 2021
can I save the file from the drive to the server storage?
netwons
commented on Oct 30, 2021
yes
jakecausier
commented on Nov 2, 2021
Unfortunately the refresh token expires after 30 or so days and required a new one to be generated, even
with the "Auto-refresh the token" checkbox enabled. It returns a 403 code.
jrnbulu
commented on Dec 5, 2021
Google\Service\Exception: {
"error": {
"errors": [
"domain": "global",
"reason": "required",
"locationType": "header",
"location": "Authorization"
],
"code": 401,
1. used secret and refresh token with double quote in .env file.
2. Required configuration done at Google side
3. calling Storage::cloud()->put("abc.png", $itemimage->imagedata); from a service file directly.
4. code for GoogleDriveServiceProvider
setClientId($config['clientId']);
$client->setClientSecret($config['clientSecret']);
$client-
>refreshToken($config['refreshToken']);
$service = new \Google_Service_Drive($client);
$adapter = new
\Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter($service, $config['folderId']);
return new
\League\Flysystem\Filesystem($adapter);
});
}
/**
* Register the application services.
*
* @return void
*/
public
function register()
{
//
}
}
5. app.php and filesystem.php required changes done.
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 25/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub
ChristianJAYs
commented on Dec 14, 2021
riosantv
commented on Dec 15, 2021
@ChristianJAYs It will me more clear If you can show your "app\Providers\GoogleDriveServiceProvider." file.
ptucky
commented 11 days ago
https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 26/26