Google Drive integration with Laravel

Google Drive integration with Laravel

Published: June 10, 2021

Updated: August 17, 2021

google drive

laravel

storage

files

In a recent project for a client, we had to integrate Laravel with Google Drive, as the main filesystem. We found many solutions for creating a new file in Laravel, but it was very difficult or almost impossible to get the id of that file. Google Drive recognizes the files through their IDs and not their names.

Before starting to code, we need to setup the connection with Google Drive API, and we can do that easily by looking at the following link here. The guide is very easy to follow and it even shows how to create a new file in your connected Google Drive folder. If that's all you need, skip reading this article.

In case you need to upload files, get their id, delete or change their names, feel free to continue reading. We will assume that you have finished the setup and managed to create a simple file on your Google Drive, as shown on the article.

We have used Laravel Facades to refactor our code, but we won't get into that now, to keep this article simple. All the coding will be done inside a controller, so create a controller and start coding. Let's say that you are sending a request from your create view, and now we are accepting that request on our store function.

Just like in the article, we need the following code to establish the connection with Google Drive:

$client = new \Google_Client();
$client->setClientId(env('GOOGLE_DRIVE_CLIENT_ID'));
$client->setClientSecret(env('GOOGLE_DRIVE_CLIENT_SECRET'));
$client->refreshToken(env('GOOGLE_DRIVE_REFRESH_TOKEN'));
$service = new \Google_Service_Drive($client);

In our project requirement, we need to create a folder for each client. We also need to have a main folder different from the root Google Drive folder, that's why we use the 'parents', which takes the value of the folder from the env file. We save the folderID of this client so that we can later navigate there or delete it if needed.

$fileMetadata = new \Google_Service_Drive_DriveFile(array(
            'name' => 'your folder name',
            'parents' => array(env('GOOGLE_DRIVE_FOLDER_ID')),
            'mimeType' => 'application/vnd.google-apps.folder'));

$folderId = $service->files->create($fileMetadata, array(
            'fields' => 'id'));

This is how the folder structure will look like after implementing the code:

"GoogleDriveRootFolder/ourMainFolder/ourNewClientFolder/ourfile.jpg"

After creating the client folder, we can upload our files there. We can use a simple html form to upload the file and in our case the input name is "myfile". We can skip the first line of code if we plan to rename the file, but we should include it if we want to use the original file name. We have a special folder for each client, and that's why we include the folderId in the 'parents' section.

$fileName = $request->file('myfile')->getClientOriginalName();

$fileMetadata = new \Google_Service_Drive_DriveFile(array(
            'name' => $fileName,
            'parents' => array($folderId)
        ));

$file = file_get_contents($request->file('myfile'));

$fileId= $service->files->create($fileMetadata, array(
            'data' => $file,
            'mimeType' => 'image/jpeg',
            'uploadType' => 'multipart',
            'fields' => 'id'));

Done, our file is successfully uploaded in the folder that we created a step before. Now, if we want to delete that file, we can make use of the Laravel Storage facade, taking into consideration that you did what was suggested in the article.

Storage::disk('google')->delete($item->items['fileId']);

That's it..!

Note: The refresh token provided by Google with be removed after 1 week. You must publish your app if you want to use it without the need of generating a new refresh token each week.