spatie/flysystem-google-cloud-storage
Google Cloud Storage adapter for Flysystem v1 (PHP 8). A maintained fork adding modern PHP support and merged fixes. Use it to connect Flysystem’s filesystem API to GCS with Google’s client authentication.
This package contains a Google Cloud Storage driver for Flysystem v1.
(If you need Google Cloud Storage support on Flysystem v2 or above (or Laravel 9), this package is not for you)
This package is a fork from superbalist/flysystem-google-cloud-storage. Changes include:
We invest a lot of resources into creating best in class open source packages . You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/flysystem-google-cloud-storage
Before you can use the package, you'll need to authenticate with Google. When possible, the credentials will be auto-loaded by the Google Cloud Client.
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); to set the location of your credentials file.%APPDATA%/gcloud/application_default_credentials.json$HOME/.config/gcloud/application_default_credentials.jsonUsing this in a Kubernetes cluster? Take a look at Workload Identity.
Here's an example that shows you have you can call the various functions to manipulate files on Google Cloud.
use Google\Cloud\Storage\StorageClient;
use League\Flysystem\Filesystem;
use Spatie\GoogleCloudStorageAdapter\GoogleCloudStorageAdapter;
$storageClient = new StorageClient([
'projectId' => 'your-project-id',
// The credentials can manually be specified by passing in a keyFilePath.
// 'keyFilePath' => '/path/to/service-account.json',
]);
$bucket = $storageClient->bucket('your-bucket-name');
$adapter = new GoogleCloudStorageAdapter($storageClient, $bucket);
$filesystem = new Filesystem($adapter);
$filesystem->write('path/to/file.txt', 'contents');
$filesystem->update('path/to/file.txt', 'new contents');
$contents = $filesystem->read('path/to/file.txt');
$exists = $filesystem->has('path/to/file.txt');
$filesystem->delete('path/to/file.txt');
$filesystem->rename('filename.txt', 'newname.txt');
$filesystem->copy('filename.txt', 'duplicate.txt');
$filesystem->deleteDir('path/to/directory');
See https://flysystem.thephpleague.com/v1/docs/usage/filesystem-api/ for full list of available functionality
You can configure this adapter to use a custom storage URI. Read more about configuring a custom domain for Google Cloud Storage here.
When using a custom storage URI, the bucket name will not prepended to the file path:
use Google\Cloud\Storage\StorageClient;
use League\Flysystem\Filesystem;
use Spatie\GoogleCloudStorageAdapter\GoogleCloudStorageAdapter;
$storageClient = new StorageClient([
'projectId' => 'your-project-id',
]);
$bucket = $storageClient->bucket('your-bucket-name');
$adapter = new GoogleCloudStorageAdapter($storageClient, $bucket);
// URI defaults to "https://storage.googleapis.com":
$filesystem = new Filesystem($adapter);
$filesystem->getUrl('path/to/file.txt');
// "https://storage.googleapis.com/your-bucket-name/path/to/file.txt"
// Using custom storage uri:
$adapter->setStorageApiUri('http://example.com');
$filesystem = new Filesystem($adapter);
$filesystem->getUrl('path/to/file.txt');
// "http://example.com/path/to/file.txt"
// You can also prefix the file path if needed:
$adapter->setStorageApiUri('http://example.com');
$adapter->setPathPrefix('extra-folder/another-folder/');
$filesystem = new Filesystem($adapter);
$filesystem->getUrl('path/to/file.txt');
// "http://example.com/extra-folder/another-folder/path/to/file.txt"
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?