How to use Google Drive storage as filesystem in Laravel
https://robindirksen.com/blog/google-drive-storage-as-filesystem-in-laravel
Last updated
https://robindirksen.com/blog/google-drive-storage-as-filesystem-in-laravel
Last updated
Published 20 September 2020 09:06 (8-minute read)
Table of contents
Limited time offer: Get 10 free Adobe Stock images.ads via Carbon
For some projects, we use Google Drive (in combination with a personal @gmail.com account or G Suite account) as a storage provider in Laravel projects. In this post, I'll show how you can set up Google Drive as a storage disk via the Laravel filesystem.
First I'll cover how you set up Google Drive as an external disk in your Laravel application. After that the process of requesting a client for Google Drive via Google API console.
Also, it's possible to use multiple Google Drive connections at the same time as separate disks, that's the latest chapter in the post.
I'm using an external package, "nao-pon/flysystem-google-drive" for this. Version 1.1 uses the Google Drive API V3, which at the time of writing is not deprecated whereas Google Drive API v2 is.
With the power of the Laravel filesystem, it is possible to set up multiple storage disks that can be used within the same application, but in this example, I'm showing you the basic setup.
In your .env file you need to set the correct credentials to connect to Google Drive APIs.
In the config/filesystems.php you need to define a storage disk that links the provider with the needed credentials stored in your .env.
We create a custom service provider in app/Providers/GoogleDriveServiceProvider.php where we extend the storage with a custom filesystem driver.
We need to register our GoogleDriveServiceProvider in config/app.php.
Now your Laravel application is ready to connect to Google Drive, but to authenticate you have to request a client from the Google API console.
This chapter is split into three separate chapters.
Create Google API Client
Get the refresh token
Get the folder id
To request an API client from Google you have to log in to Google Cloud Console and create a project (if you don't have one, it may take some seconds to create).
Next, you can search for the "Google Drive API".
If you don't have the Google Drive API enabled in your Google Cloud project, you need to enable it (may take some seconds to enable).
After you've enabled the Google Drive API you will be redirected to a view where you see some details/statistics about the Google Drive AP. You have to make an OAuth client, this can be done under the "credentials" tab.
At the top of the screen you can create new credentials, you need to use "OAuth client ID".
It's also possible to make a "Service account", but then it's not possible to authenticate multiple users. A "service account" needs to be added to the folder/file/Shared Drive to use it.
You are creating an OAuth client for your usage, so it's not needed to get it reviewed by Google.
In our example, we defined the following details for the OAuth client.
Application type: "web application"
Name: "web client 3" (or something you can recognise)
URIs: "https://developers.google.com/oauthplayground"
After you created the OAuth client you receive 2 keys, those keys are needed to authenticate the user a user for your application. You have to store these items in the .env file:
The "Client Secret" is only visible once, after you closed the modal it's not possible to see anymore.
Now the client is created and you can continue to the next step, where you execute the actual authentication.
To get the refresh token we use Google OAuth playground, that's an easy way to test and get your authentication credentials for connecting to the Google APIs.
Go to developers.google.com/oauthplayground and configure the playground to use your Google API client created in step 1.
Next, you have to select the needed API ("Drive API V3" > "https://www.googleapis.com/auth/drive") and click on the "Authorize APIs" button. This will redirect you to a screen where you give permissions to the client (created in step 1) to access your Google Drive.
You may see a screen: "OAuth client is not validated by Google and may be insecure, are you sure you want to continue". You can continue because you made the client yourself.
After the authentication you will be redirected to the OAuth playground where you see the "authorization code" filled, this is a code that is valid for 30 seconds. This code can be used one time to get the refresh & access token.
This can be done using the "exchange authorization code for tokens".
You will receive the "refresh token" & "access token". The "access token" can be used for 3600 seconds and after that, it's not valid anymore.
With the "refresh token" you can receive a new "access token" that's valid for the next 3600 seconds.
Copy the "refresh token" to your .env file (in the example we use the MAIN_GOOGLE_DRIVE_REFRESH_TOKEN key). The package uses this key to get an access token to connect to your Google Drive via the APIs.
To select in which folder (or Shared Drive) in Google Drive your application has to store its data you must provide a folder id. This folder id can you get from the URL within Google Drive. Therefore you go to drive.google.com, select the wanted place, and look at the URL. Take the part highlighted in the following images and store them in your .env file.
Detailed instructions on how to get the Google Drive File, Shared Drive, or Folder ID.
In our case this is the folder id: "1jH09RbtkNC8dvpo_Xji8eAVhlTh5-T0f".
From that moment your application can use that folder as a disk in your application.
You can set the main cloud disk via .env:
Or you can use it programmatically in your applications:
When you want to use Shared Drives (or Team Drives) you have to provide an extra parameter "teamDriveId" in the config. The config may look like:
It's possible to use multiple Google Drive connections at the same time in your Laravel applications. Therefore you need to add a second disk in config/filesystems.php. For example, it can look like this:
And after you set the correct SECOND_GOOGLE disk credentials in your .env you can use the second disk as:
Do you have something to add to this article? Let me know!