Laravel & Google Drive Storage - Demo project with Laravel 6.x and earlier

Overview

Laravel & Google Drive Storage

Demo project with Laravel 8.X

Look at the commit history to see each of the steps I have taken to set this up.

Set up this demo project locally

git clone [email protected]:erikn69/laravel-google-drive-demo.git
git checkout 8.x
composer install
cp .env.example .env
php artisan key:generate

I took care of this:

This will also install only one additional package that is not included by Laravel out of the box:

"masbug/flysystem-google-drive-ext":"^1.0"

I have included GoogleDriveServiceProvider which I have added to the providers array in config/app.php, and added a google disk in config/filesystems.php:

'disks' => [

    // ...

    'google' => [
        'driver' => 'google',
        'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
        'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
        'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
        'folder' => env('GOOGLE_DRIVE_FOLDER'),
        // 'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'),
    ],

    // ...

],

Create your Google Drive API keys

Detailed information on how to obtain your API ID, secret and refresh token:

Update .env file

Add the keys you created to your .env file and set google as your default cloud storage. You can copy the .env.example file and fill in the blanks.

FILESYSTEM_CLOUD=google
GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=xxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxx
GOOGLE_DRIVE_FOLDER=folder_name
#GOOGLE_DRIVE_TEAM_DRIVE_ID=xxx

Using multiple Google Drive accounts

If you want to use multiple Google Drive accounts in a single Laravel app, you need to get the API keys for each one as described above and store them separately in your .env file:

MAIN_GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
MAIN_GOOGLE_DRIVE_CLIENT_SECRET=xxx
MAIN_GOOGLE_DRIVE_REFRESH_TOKEN=xxx
MAIN_GOOGLE_DRIVE_FOLDER=

BACKUP_GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
BACKUP_GOOGLE_DRIVE_CLIENT_SECRET=xxx
BACKUP_GOOGLE_DRIVE_REFRESH_TOKEN=xxx
BACKUP_GOOGLE_DRIVE_FOLDER=

Then you should add a disk in config/filesystems.php for each account using the google driver and the account specific keys:

'main_google' => [
    'driver' => 'google',
    'clientId' => env('MAIN_GOOGLE_DRIVE_CLIENT_ID'),
    'clientSecret' => env('MAIN_GOOGLE_DRIVE_CLIENT_SECRET'),
    'refreshToken' => env('MAIN_GOOGLE_DRIVE_REFRESH_TOKEN'),
    'folder' => env('MAIN_GOOGLE_DRIVE_FOLDER'),
],

'backup_google' => [
    'driver' => 'google',
    'clientId' => env('BACKUP_GOOGLE_DRIVE_CLIENT_ID'),
    'clientSecret' => env('BACKUP_GOOGLE_DRIVE_CLIENT_SECRET'),
    'refreshToken' => env('BACKUP_GOOGLE_DRIVE_REFRESH_TOKEN'),
    'folder' => env('BACKUP_GOOGLE_DRIVE_FOLDER'),
],

Now you can access the drives like so:

$mainDisk = Storage::disk('main_google');
$backupDisk = Storage::disk('backup_google');

Keep in mind that there can only be one default cloud storage drive, defined by FILESYSTEM_CLOUD in your .env (or config) file. If you set it to main_google, that will be the cloud drive:

Storage::cloud(); // refers to Storage::disk('main_google')

Available routes

Route Description
/put Puts a new test.txt file to Google Drive
/put-existing Puts an existing file to Google Drive
/list-files Lists all files in Google Drive (root directory, not recursive by default)
/list-folder-contents List all files in a specific folder(Test Dir)
/list-team-drives Lists all team drives in Google Drive
/get Finds and downloads the test.txt file from Google Drive
/create-dir Creates a Test Dir directory
/create-sub-dir Creates a Test Dir directory and a Sub Dir sub directory
/put-in-dir Puts a new test.txt file to the Test Dir sub directory
/delete Delete a file from Google Drive
/delete-dir Delete an entire directory from Google Drive
/rename-dir Rename a directory in Google Drive
/put-get-stream Use a stream to store and get larger files
/share Change permissions to share a file with the public
/export/{filename} Export a Google doc (to PDF)

This is a very basic example to get you started. To see the logic behind these routes, check routes/web.php.

You might also like...
This Repo is a storage of Postman collections for Magento

Magento Postman repository This Repository is a storage of Postman collections for Magento. If you have what to share, you are welcome to contribute a

File Storage Api

flux-file-storage-api File Storage Api Installation Native Download RUN (mkdir -p /%path%/libs/flux-file-storage-api && cd /%path%/libs/flux-file-stor

Microsoft Azure Storage Library for PHP

Microsoft Azure Storage PHP Client Libraries This project will be in Community Support and Azure Storage team commits to validate and release every qu

Lumen on Docker - Skeleton project with Nginx, MySQL & PHP 8 | Aws ECS, Google Kubernates, Azure Container Engine

Docker infrastructure for Lumen Description Microservice Lumen is a starting skeleton based on Docker and Lumen Framework. This project helps to devel

Pat if amp - ⚡ A Textpattern Conditional Plugin for Google's Accelerated Mobile Pages Project (AMP)
Pat if amp - ⚡ A Textpattern Conditional Plugin for Google's Accelerated Mobile Pages Project (AMP)

pat_if_amp Download | Packagist AMP pages for Textpattern CMS. This conditional tag examines the URL of the current page and determines if the URL end

The slides and demo files for my Alpine Day 2021 talk

Building a Better Dialog Austen Cameron - @austencam This repository contains the slides and demos for my talk from Alpine Day 2021. Below, you'll fin

Demo serverless applications, examples code snippets and resources for PHP
Demo serverless applications, examples code snippets and resources for PHP

The Serverless LAMP stack Examples Code example Description AWS blog link 0.1-SimplePhpFunction A very simple implementation of a PHP Lambda function.

Demo Silverstripe and JavaScript sources for Lightning Talk
Demo Silverstripe and JavaScript sources for Lightning Talk "FormField Mini Apps" at StripeCon EU 2022

Watch the Lightning Talk on Youtube 📺 Demo repository for Lightning Talk "FormField Mini Apps with the JavaScript framework/lib/style of your choice"

Vulnerable demo application for the race condition

Vulnerable PHP App (Race Condition) Environment setup: docker-compose up Environment verification: Connection Test: http://localhost/test.php Vulnera

Comments
  • Working within Shared Drives

    Working within Shared Drives

    Hello - I'm thinking this is less of an issue and more of just me not understanding but I'm trying to work within Shared Drives and not having any luck. The structure I'm going for is:

    Shared Drive

    • Sub Folder 1
      • File 1
    • Sub Folder 2
      • File 1
      • File 2

    I've been able to use Storage::disk('google')->makeDirectory('Sub Folder 1') and that works fine. The problem comes when I try to use Storage::disk('google')->put('Sub Folder 1/File 1.txt', 'Hello World'); - In this case it ends up creating a second "Sub Folder 1" and then I get the error in Laravel saying "Unable to read file from location: [shared drive id]/Sub Folder 1. File not found"

    How do I go about utilizing the standard Laravel Storage Facade inside of folders within a shared drive?

    Thanks in advance!

    opened by rjshreve 15
Owner
null
Like Cloud Drive Run on Raspbian + Nginx + PHP

Raspberry-Pi-Cloud Like Cloud Drive Run on Raspbian + Nginx + PHP I Made a Project Called Raspberry-Pi-Cloud. it's on testing stage help me to test it

null 1 Dec 25, 2021
Google Cloud Storage for PHP

Google Cloud Storage for PHP Idiomatic PHP client for Cloud Storage. API documentation NOTE: This repository is part of Google Cloud PHP. Any support

Google APIs 281 Nov 10, 2022
Demo project for the API Platform / DDD Workshop

Workshop DDD x API Platform This is a demo project used for the DDD x API Platform Workshop by @chalasr & @mtarld from @coopTilleuls. Checkout git clo

Les-Tilleuls.coop 34 Dec 16, 2022
A simple mailable trait and interface to export mails to a storage disk once being sent.

Laravel Mail Export This package can export any mail sent with Laravel's Mailable class to any desired filesystem disk and path as a .eml file. This c

Pod Point 80 Nov 6, 2022
Greyhole uses Samba to create a storage pool of all your available hard drives, and allows you to create redundant copies of the files you store.

Greyhole Greyhole is an application that uses Samba to create a storage pool of all your available hard drives (whatever their size, however they're c

Guillaume Boudreau 245 Dec 18, 2022
File uploads with validation and storage strategies

Upload This component simplifies file validation and uploading. Usage Assume a file is uploaded with this HTML form: <form method="POST" enctype="mult

Brandon Savage 1.7k Dec 27, 2022
Roach-example-project - Example project to demonstrate how to use RoachPHP in a Laravel project.

Example repository to illustrate how to use roach-php/laravel in a Laravel app. Check app/Spiders/FussballdatenSpider.php for an example spider that c

Kai Sassnowski 11 Dec 15, 2022
A Qiniu Storage filesystem for Laravel

Laravel filesystem Qiniu Qiniu storage for Laravel based on overtrue/flysystem-qiniu. Requirement PHP >= 5.5.9 Installation $ composer require "overtr

安正超 463 Dec 6, 2022
Upload attachments to content storage platform like Aliyun OSS, Tencent COS

Overview Yun storage provides a layer that mediates between a user or configured storage frontend and one or several storage backends. Note: jichangfe

Changfeng Ji 2 Oct 13, 2021
High-performance, low-memory-footprint, single-file embedded database for key/value storage

LDBA - a fast, pure PHP, key-value database. Information LDBA is a high-performance, low-memory-footprint, single-file embedded database for key/value

Simplito 12 Nov 13, 2022