Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Superbalist/flysystem-google-cloud-storage
Flysystem Adapter for Google Cloud Storage
https://github.com/Superbalist/flysystem-google-cloud-storage
flysystem flysystem-adapter flysystem-google-cloud-storage google-cloud google-cloud-platform google-cloud-storage google-storage php superbalist
Last synced: 4 months ago
JSON representation
Flysystem Adapter for Google Cloud Storage
- Host: GitHub
- URL: https://github.com/Superbalist/flysystem-google-cloud-storage
- Owner: Superbalist
- License: mit
- Created: 2015-10-16T07:58:24.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-10-31T02:39:42.000Z (over 1 year ago)
- Last Synced: 2024-09-08T01:51:47.586Z (5 months ago)
- Topics: flysystem, flysystem-adapter, flysystem-google-cloud-storage, google-cloud, google-cloud-platform, google-cloud-storage, google-storage, php, superbalist
- Language: PHP
- Size: 80.1 KB
- Stars: 265
- Watchers: 44
- Forks: 104
- Open Issues: 31
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# flysystem-google-cloud-storage
A Google Cloud Storage adapter for [flysystem](https://github.com/thephpleague/flysystem) - a PHP filesystem abstraction.
[](https://twitter.com/superbalist)
[](https://travis-ci.org/Superbalist/flysystem-google-cloud-storage)
[](https://styleci.io/repos/44370843)
[](LICENSE)
[](https://packagist.org/packages/superbalist/flysystem-google-storage)
[](https://packagist.org/packages/superbalist/flysystem-google-storage)## Installation
```bash
composer require superbalist/flysystem-google-storage
```## Integrations
Want to get started quickly? Check out some of these integrations:
* Laravel - https://github.com/Superbalist/laravel-google-cloud-storage
## Usage
```php
use Google\Cloud\Storage\StorageClient;
use League\Flysystem\Filesystem;
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter;/**
* The credentials will be auto-loaded by the Google Cloud Client.
*
* 1. The client will first look at the GOOGLE_APPLICATION_CREDENTIALS env var.
* You can use ```putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');``` to set the location of your credentials file.
*
* 2. The client will look for the credentials file at the following paths:
* - windows: %APPDATA%/gcloud/application_default_credentials.json
* - others: $HOME/.config/gcloud/application_default_credentials.json
*
* If running in Google App Engine, the built-in service account associated with the application will be used.
* If running in Google Compute Engine, the built-in service account associated with the virtual machine instance will be used.
*/$storageClient = new StorageClient([
'projectId' => 'your-project-id',
]);
$bucket = $storageClient->bucket('your-bucket-name');$adapter = new GoogleStorageAdapter($storageClient, $bucket);
$filesystem = new Filesystem($adapter);
/**
* The credentials are manually specified by passing in a keyFilePath.
*/$storageClient = new StorageClient([
'projectId' => 'your-project-id',
'keyFilePath' => '/path/to/service-account.json',
]);
$bucket = $storageClient->bucket('your-bucket-name');$adapter = new GoogleStorageAdapter($storageClient, $bucket);
$filesystem = new Filesystem($adapter);
// write a file
$filesystem->write('path/to/file.txt', 'contents');// update a file
$filesystem->update('path/to/file.txt', 'new contents');// read a file
$contents = $filesystem->read('path/to/file.txt');// check if a file exists
$exists = $filesystem->has('path/to/file.txt');// delete a file
$filesystem->delete('path/to/file.txt');// rename a file
$filesystem->rename('filename.txt', 'newname.txt');// copy a file
$filesystem->copy('filename.txt', 'duplicate.txt');// delete a directory
$filesystem->deleteDir('path/to/directory');// see http://flysystem.thephpleague.com/api/ for full list of available functionality
```## Google Storage specifics
When using a custom storage uri the bucket name will not prepended to the file path.
```php
$storageClient = new StorageClient([
'projectId' => 'your-project-id',
]);
$bucket = $storageClient->bucket('your-bucket-name');
$adapter = new GoogleStorageAdapter($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"// set 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"
```