0% found this document useful (0 votes)
381 views

Setup A Laravel Storage Driver With Google Drive API GitHub

This document provides instructions for setting up a Laravel storage driver that uses the Google Drive API to store and retrieve files from a Google Drive account. It describes how to get client ID, client secret, and refresh token from the Google API console. It also explains how to install the Flysystem adapter for Google Drive and configure Laravel filesystem to use it for the "google" disk. Code samples are given to authenticate with Google and test file operations on Drive.

Uploaded by

Michael LTR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
381 views

Setup A Laravel Storage Driver With Google Drive API GitHub

This document provides instructions for setting up a Laravel storage driver that uses the Google Drive API to store and retrieve files from a Google Drive account. It describes how to get client ID, client secret, and refresh token from the Google API console. It also explains how to install the Flysystem adapter for Google Drive and configure Laravel filesystem to use it for the "google" disk. Code samples are given to authenticate with Google and test file operations on Drive.

Uploaded by

Michael LTR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub

Instantly share code, notes, and snippets.

sergomet / GoogleDriveServiceProvider.php
Forked from ivanvermeyen/!NOTE.md
Created 5 years ago


Star


Code
Revisions
7
Stars
66
Forks
25

Setup a Laravel Storage driver with Google Drive API

README.md

Setup a Laravel Storage driver with Google


Drive API
Log in to your Google Account and go to this website:

https://console.developers.google.com/

Getting your Client ID and Client Secret


Create a new project using the dropdown at the top.

After you enter a name, it takes a few seconds before the project is successfully created on
the server.

Make sure you have the project selected at the top.

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

And then Enable it.

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

Choose "Web Application" and give it a name.

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.

Getting your Refresh Token


Now head to https://developers.google.com/oauthplayground.

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

Getting your Folder ID


If you want to store files in your Google Drive root directory, then the folder ID can be
null .
Else go into your Drive and create a folder.

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

Pull in Flysystem Adapter for Google Drive:

https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 8/26
1/18/22, 4:22 PM Setup a Laravel Storage driver with Google Drive API · GitHub

composer require nao-pon/flysystem-google-drive:~1.1

Add the storage disk configuration to config/filesystem.php :

return [

// ...

'cloud' => 'google', // Optional: set Google Drive as default cloud storage

'disks' => [

// ...

'google' => [
'driver' => 'google',

'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),

'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),

'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),

'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),

],

// ...

],

// ...

];

And update your .env file:

GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com

GOOGLE_DRIVE_CLIENT_SECRET=xxx

GOOGLE_DRIVE_REFRESH_TOKEN=xxx

GOOGLE_DRIVE_FOLDER_ID=null

Save GoogleDriveServiceProvider.php to app/Providers and add it to the providers array


in config/app.php :

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

Now you should be up and running:

Route::get('test', function() {

Storage::disk('google')->put('test.txt', 'Hello World');

});

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

thank you great job man

this tutorial work with laravel 5.6 :)

idcProger
commented on Aug 8, 2018

{\n

"error": {\n

"errors": [\n

{\n

"domain": "global",\n

"reason": "required",\n

"message": "Login Required",\n

"locationType": "header",\n

"location": "Authorization"\n

}\n

],\n

"code": 401,\n

"message": "Login Required"\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

you have to add https://developers.google.com/oauthplayground on authorized redirect URLs, then include


your Client ID and Client Secret on Use your own OAuth credentials. It'll work.

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

Google_Service_Exception thrown with message "{

"error": {

"errors": [

"domain": "global",

"reason": "insufficientPermissions",

"message": "Insufficient Permission: Request had insufficient authentication scopes."

],

"code": 403,

"message": "Insufficient Permission: Request had insufficient authentication scopes."

"

sergomet
commented on Oct 22, 2019 Author

@afrasiyabhaider recheck your credentials

sammak1432
commented on Nov 18, 2019

edited

"error": {

"errors": [

"domain": "global",

"reason": "required",

"message": "Login Required",

"locationType": "header",

"location": "Authorization"

],

"code": 401,

"message": "Login Required"

Getting the same issue.

I've double check credentials.

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

how to get that image or file from google drive???

JuanAdamuz
commented on Dec 19, 2019

how to get that image or file from google drive???

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:

replace the boot function in GoogleDriveService:

$client = new \Google_Client();

$client->setAccessType('offline');

$client->setClientId($config['clientId']);

$client->refreshToken($config['refreshToken']);

$client->fetchAccessTokenWithRefreshToken($config['refreshToken']); <--- This save my life

$service = new \Google_Service_Drive($client);

$adapter = new \Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter($service, $config['folderId']);

return new \League\Flysystem\Filesystem($adapter);

I hope it'll worked for u

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.

Running Storage::files or Storage::allFiles returns

["1ci2uGHETqRSAsojJKFXIqrVSrxLs4xgv","1Ja081Rq8nf-2LlL-UsJA0k49v4Ek47_D"]

Since it does this, I cannot properly recognize each file.

The original file names are sdfsdfsdf.txt and test.txt

Attempting to run Storage::get('sdfsdfsdf.txt') and Storage::get('test.txt') returns a file not found


exception.

Also, I generated these files by Storage::put('test.txt', 'Hello World'); and return


Storage::put('sdfsdfsdf.txt', 'Hello World');

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

Hello I'm trying to upload but I get this error

{↵ "error": {↵ "errors": [↵ {↵ "domain": "global",↵ "reason": "notFound",↵ "message": "File not found: image.",↵
"locationType": "parameter",↵ "location": "fileId"↵

}↵

],↵ "code": 404,↵ "message": "File not found: image."↵

}↵

}↵"

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.

Running Storage::files or Storage::allFiles returns

["1ci2uGHETqRSAsojJKFXIqrVSrxLs4xgv","1Ja081Rq8nf-2LlL-UsJA0k49v4Ek47_D"]

Since it does this, I cannot properly recognize each file.

The original file names are sdfsdfsdf.txt and test.txt

Attempting to run Storage::get('sdfsdfsdf.txt') and Storage::get('test.txt') returns a file not


found exception.

Also, I generated these files by Storage::put('test.txt', 'Hello World'); and return


Storage::put('sdfsdfsdf.txt', 'Hello World');

I'm not sure if there is a special parameter I missed, but I couldn't find a similar occurrence of
randomized file names.

Use method listContents() instead.

Check public methods in https://github.com/nao-pon/flysystem-google-


drive/blob/master/src/GoogleDriveAdapter.php

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.

Please guide me with the 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

Error: refresh token must be passed in or set as part of setAccessToken

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

this my code in model to get img

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

this my code in model to get img

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>);

});

3. You can add the above path to the image source.

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

How to append another params? like "&includeItemsFromAllDrives=true"?

riosantv
commented on Nov 27, 2020

@santoshupadhyay clear your config cache

by running

php artisan config:cache

riosantv
commented on Nov 27, 2020

is there a way to get uploaded file id in this package?

mahmudtopu3
commented on Dec 1, 2020

Hello I'm trying to upload but I get this error

{↵ "error": {↵ "errors": [↵ {↵ "domain": "global",↵ "reason": "notFound",↵ "message": "File not found:
image.",↵ "locationType": "parameter",↵ "location": "fileId"↵

}↵

],↵ "code": 404,↵ "message": "File not found: image."↵

}↵

}↵"

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

Hello I'm trying to upload but I get this error

{↵ "error": {↵ "errors": [↵ {↵ "domain": "global",↵ "reason": "notFound",↵ "message": "File not found:
image.",↵ "locationType": "parameter",↵ "location": "fileId"↵

}↵

],↵ "code": 404,↵ "message": "File not found: image."↵

}↵

}↵"

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

You can simpy do this

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

});

but not save to googledrive & not error


Jangbe
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:

***@***.**** commented on this gist.
------------------------------
this is mycode
Route::post('/upload',function(\Illuminate\Http\Request $request){
dd( $request->file('thing')-
>store('google'));
});
but not save to googledrive & not error

You are receiving this because you
commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/f234cc7a8351352170eb547cccd65011#gistcomment-3559974>,
or unsubscribe
<https://github.com/notifications/unsubscribe-
auth/ANU4J7XSDY4CP5YXBU3PGVTSUTITLANCNFSM4HILSNYA>
.

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:

@.**** commented on this gist. ------------------------------ this is mycode


Route::post('/upload',function(\Illuminate\Http\Request $request){ dd( $request->file('thing')-
>store('google')); }); but not save to googledrive & not error — You are receiving this because you
commented. Reply to this email directly, view it on GitHub
https://gist.github.com/f234cc7a8351352170eb547cccd65011#gistcomment-3559974, or unsubscribe
https://github.com/notifications/unsubscribe-
auth/ANU4J7XSDY4CP5YXBU3PGVTSUTITLANCNFSM4HILSNYA .

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

yes but not work

Jangbe
commented on Dec 14, 2020

edited

try to write this..

$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

Now how to import the desired file into Google doc?

Jangbe
commented on Dec 17, 2020

@Jangbe

Now how to import the desired file into Google doc?

I don't know, I've never tried it

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,

"message": "Login Required" }

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

AWS S3 > Google Cloud Storage

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

"message": "Login Required",\n


"locationType": "header",\n

"location": "Authorization"\n

}\n

],\n

"code": 401,\n

"message": "Login Required"\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"

this is work for me

myazidinniam
commented on Mar 18, 2021

Google\Service\Exception

{ "error":

{ "errors": [ {

"domain": "global", "reason": "required", "message": "Login Required", "locationType": "header",


"location": "Authorization" } ],

"code": 401,

"message": "Login Required" }

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"

this is work for me

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

this my code in model to get img

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>);

});

1. You can add the above path to the image source.

thank you very much !! this works

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

Error 403: access_denied

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

Thank you. I will try soon.

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?

Can anyone please let me know why I'm getting this

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?

Can anyone please let me know why I'm getting this

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

try to write this..

$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

Storage::disk('google')->put('filename.jpg', fopen($request->file('name'),'r+'), 'public'); //source : i have


researched ref code

above line worked for me,

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.

Any idea on how to bypass this?

jrnbulu
commented on Dec 5, 2021

Stuck with below details. Any help!!!

Google\Service\Exception: {

"error": {

"errors": [

"domain": "global",

"reason": "required",

"message": "Login Required",

"locationType": "header",

"location": "Authorization"

],

"code": 401,

"message": "Login Required"

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

I am getting an error of: "ErrorException Undefined index:" on the


"app\Providers\GoogleDriveServiceProvider." which is the Client ID. Does anyone knows how to solve this?
Thank you

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

Cool Thanks for this guide. Save my life ^_^

https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011 26/26

You might also like