ntsoluzioninformatiche / laravel-google-cloud-storage
A Google Cloud Storage filesystem for Laravel
This package's canonical repository appears to be gone and the package has been frozen as a result.
Requires
- php: >=5.5.9
- illuminate/filesystem: ^5.1|^5.2|^5.3
- illuminate/support: ^5.1|^5.2|^5.3
- superbalist/flysystem-google-storage: >=3.0 <8.0
Requires (Dev)
- phpunit/phpunit: >=4.8
This package is not auto-updated.
Last update: 2019-10-28 16:08:42 UTC
README
A Google Cloud Storage filesystem for Laravel.
This package is a wrapper bridging flysystem-google-storage into Laravel as an available storage disk.
Installation
composer require ntsoluzioninformatiche/laravel-google-cloud-storage
In Laravel < 5.5
Register the service provider in app.php
'providers' => [ // ... Ntsoluzioninformatiche\LaravelGoogleCloudStorage\GoogleCloudStorageServiceProvider::class, ]
Add a new disk to your filesystems.php
config
'gcs' => [ 'driver' => 'gcs', 'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'), 'key_file' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json 'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'), 'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket 'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below 'visibility' => 'public', // optional: public|private ],
Authentication
The Google Client uses a few methods to determine how it should authenticate with the Google API.
-
If you specify a
key_file
in the disk config, that json credentials file will be used. -
If the
GOOGLE_APPLICATION_CREDENTIALS
env var is set, it will use that.putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
-
It will then try load the key file from a 'well known path':
- 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.
Public URLs
The adapter implements a getUrl($path)
method which returns a public url to a file.
Note: Method available for Laravel 5.2 and higher. If used on 5.1, it will throw an exception.
$disk = Storage::disk('gcs'); $url = $disk->url('folder/my_file.txt'); >>> http://storage.googleapis.com/bucket-name/folder/my_file.txt
If you configure a path_prefix
in your config:
$disk = Storage::disk('gcs'); $url = $disk->url('folder/my_file.txt'); >>> http://storage.googleapis.com/bucket-name/path-prefix/folder/my_file.txt
If you configure a custom storage_api_uri
in your config:
$disk = Storage::disk('gcs'); $url = $disk->url('folder/my_file.txt'); >>> http://your-custom-domain.com/bucket-name/path-prefix/folder/my_file.txt
For a custom domain (storage api uri), you will need to configure a CNAME DNS entry pointing to storage.googleapis.com
.
Please see https://cloud.google.com/storage/docs/xml-api/reference-uris#cname for further instructions.
Usage
$disk = Storage::disk('gcs'); // create a file $disk->put('avatars/1', $fileContents); // check if a file exists $exists = $disk->exists('file.jpg'); // get file modification date $time = $disk->lastModified('file1.jpg'); // copy a file $disk->copy('old/file1.jpg', 'new/file1.jpg'); // move a file $disk->move('old/file1.jpg', 'new/file1.jpg'); // get url to file $url = $disk->url('folder/my_file.txt'); // See https://laravel.com/docs/5.6/filesystem for full list of available functionality
You can also change visibility of a single file
$disk = Storage::disk('gcs'); // during a file creation $disk->put('avatars/1', $fileContents, 'private'); //after creation $disk->setVisibility('avatars/1', 'public');