Download the PHP package aslamhus/google-drive-uploader without Composer
On this page you can find all versions of the php package aslamhus/google-drive-uploader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aslamhus/google-drive-uploader
More information about aslamhus/google-drive-uploader
Files in aslamhus/google-drive-uploader
Package google-drive-uploader
Short Description A PHP class for uploading files to Google Drive, providing methods for basic and resumable uploads, with support for managing dependencies and setting up Google Drive API service accounts
License MIT
Informations about the package google-drive-uploader
Google Drive Uploader
Google Drive Uploader is a PHP class that simplifies uploading files to Google Drive using the Google Drive API. It supports basic and resumable uploads, allowing you to efficiently upload large files in chunks with options for asynchronous programming.
Google Drive API Documentation
This class relies on the Google API PHP client library and requires a Google service account with the Google Drive API enabled. Please refer to the following resources for additional information and setup instructions:
- Google Drive API Quickstart for PHP
- Google Drive API Managing Uploads
- Google API PHP Client GitHub Repository
Setting up Google Drive API Service Account
To use the Google Drive Uploader, you need to set up a Google Cloud Console service account and obtain the necessary credentials. Follow these steps:
- Create a Google Cloud project: Google Cloud Console
- Enable the Google Drive API: Google Workspace Product Library
- Set up OAuth for your app: Navigate to APIs and services > OAuth consent screen.
- Choose scopes: Select the required scopes for Google Drive API. See available scopes here.
- Create access credentials (API key): Navigate to APIs & Services > Credentials, then click "Create Credentials" > "API key."
- Create access credentials (OAuth client ID): Navigate to APIs & Services > Credentials, then click "Create Credentials" > "OAuth client ID." Add authorized URLs and download the client secret JSON file.
-
Download the service account secret: Navigate to Service accounts > Keys > Create key.
- Create a service account: Navigate to IAM & Admin > Service Accounts, then click "Create Service Account."
- Add the service account to the Google Drive folder by granting permissions to the service account email.
You're now ready to use the Google Drive Uploader!
Usage
Install
Config
Before you begin uploading you must set your GOOGLE_APPLICATION_CREDENTIALS
environment variable and create a drive folder with shared permissions granted to your service account email. Make sure you've followed the steps above if you haven't already got a service account in Google Cloud Console.
- Service account credentials
GoogleDriveUploader
sets default credentials via environment variables like so:
- Drive folder id
In order to upload a file to a Google Drive account, create a folder in your desired google drive account where you'd like your uploads to be delivered. Then grant your service account email address access to the folder. You can find your service account email in Google Cloud Console.
Once you've done this, simply copy the drive folder id from the drive folder url. You can find the drive folder url by navigating to the appropriate folder in Google Drive and then copying the id string from url:
https://drive.google.com/drive/folders/file-id-string
Basic Upload
Use this method for small files.
Resumable Upload
Use this method for large uploads and/or when the upload process has a high chance of being interrupted.
Asynchronous Resumable Upload
To perform an aysnchronous upload, use the resumable upload method.
Resume an Interrupted Upload
Callbacks
onChunkRead
You can pass an optional onChunkRead
callback method to startResumable
and resume
.
Memory Limits
The basicUpload
method loads the file into memory using file_get_contents
, so it's best not to use large files with this method. If you do, you may run into memory limit errors.
The resumable upload is performed by splitting the file into chunks which are by default 262144 bytes
. This is the Google recommended chunk size. You should be able to upload very large files without encountering any memory limits.
Exception Handling
The class includes a custom exception class, GoogleDriveUploaderException
, to handle errors in the Google Drive uploading process.
Testing
Before testing, make sure you've installed dev dependencies, and set up your .env file with the GOOGLE_APPLICATION_CREDENTIALS
and GOOGLE_DRIVE_FOLDER_ID
.
Note: running the tests will upload 3 small test videos to your good drive folder with the id you specify
Then run:
Feel free to contribute or report issues on GitHub.
Happy uploading!