All Projects → spatie → laravel-google-cloud-storage

spatie / laravel-google-cloud-storage

Licence: MIT license
Google Cloud Storage filesystem driver for Laravel

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-google-cloud-storage

GoogleCloudLogging
Swift (Darwin) library for logging application events in Google Cloud.
Stars: ✭ 24 (-64.18%)
Mutual labels:  google-cloud-platform
gcegrpc
gRPC client/server running loadbalanced/failover on Google Compute Engine and Google App Engine
Stars: ✭ 75 (+11.94%)
Mutual labels:  google-cloud-platform
cloud-cardboard-viewer
Build a Node.js & Angular 2 Web App using Google Cloud Platform
Stars: ✭ 23 (-65.67%)
Mutual labels:  google-cloud-platform
blockchain-etl-streaming
Streaming Ethereum and Bitcoin blockchain data to Google Pub/Sub or Postgres in Kubernetes
Stars: ✭ 57 (-14.93%)
Mutual labels:  google-cloud-platform
awesome-bigtable
⚡ Delightful list of Google Bigtable resources, packages and interesting finds.
Stars: ✭ 37 (-44.78%)
Mutual labels:  google-cloud-platform
gke-rbac-demo
This project covers two use cases for RBAC within a Kubernetes Engine cluster. First, assigning different permissions to user personas. Second, granting limited API access to an application running within your cluster. Since RBAC's flexibility can occasionally result in complex rules, you will also perform common steps for troubleshooting RBAC a…
Stars: ✭ 138 (+105.97%)
Mutual labels:  google-cloud-platform
K8s-Cluster-Provisioner-GCP-Terrafrom
This repo will seamlessly setup self managed Kubernetes cluster in GCP using Terraform and Kubespray.
Stars: ✭ 17 (-74.63%)
Mutual labels:  google-cloud-platform
kane
Google Pub/Sub client for Elixir
Stars: ✭ 92 (+37.31%)
Mutual labels:  google-cloud-platform
infra
Infrastructure management for Google Cloud Platform
Stars: ✭ 14 (-79.1%)
Mutual labels:  google-cloud-platform
zorya
Google Cloud Instance Scheduler helping to reduce costs by 60% on average for non-production environments.
Stars: ✭ 127 (+89.55%)
Mutual labels:  google-cloud-platform
star-dabang
[번개장터 / 스트리미 입사] 스타벅스 어플을 모티브로 한 카페 서비스
Stars: ✭ 14 (-79.1%)
Mutual labels:  google-cloud-platform
tftransform-demo
tf.Transform example for building digital twin with Apache Beam and Tensorflow
Stars: ✭ 45 (-32.84%)
Mutual labels:  google-cloud-platform
gke-istio-gce-demo
In this project, you will leverage Kubernetes Engine and Google Compute Engine to explore how Istio can manage services that reside outside of the Kubernetes Engine environment. You will deploy a typical Istio service mesh in Kubernetes Engine, then configure an externally deployed microservice to join the mesh.
Stars: ✭ 53 (-20.9%)
Mutual labels:  google-cloud-platform
drf-angular-docker-tutorial
Dockerized Django Back-end API using DRF with Angular Front-end Tutorial
Stars: ✭ 53 (-20.9%)
Mutual labels:  google-cloud-platform
emulator-tools
Google Cloud BigTable and PubSub emulator tools to make development a breeze
Stars: ✭ 16 (-76.12%)
Mutual labels:  google-cloud-platform
course-material
Course Material for in28minutes courses on Java, Spring Boot, DevOps, AWS, Google Cloud, and Azure.
Stars: ✭ 544 (+711.94%)
Mutual labels:  google-cloud-platform
Php-Google-Vision-Api
Google Vision Api for PHP (https://cloud.google.com/vision/)
Stars: ✭ 61 (-8.96%)
Mutual labels:  google-cloud-platform
Cloud-Service-Providers-Free-Tier-Overview
Comparing the free tier offers of the major cloud providers like AWS, Azure, GCP, Oracle etc.
Stars: ✭ 226 (+237.31%)
Mutual labels:  google-cloud-platform
iris3
An upgraded and improved version of the Iris automatic GCP-labeling project
Stars: ✭ 38 (-43.28%)
Mutual labels:  google-cloud-platform
google-cloud-cpp-spanner
C++ client library for Google Cloud Spanner
Stars: ✭ 26 (-61.19%)
Mutual labels:  google-cloud-platform

Google Cloud Storage filesystem driver for Laravel

Latest Version on Packagist run-tests GitHub Code Style Action Status Total Downloads


Google Cloud Storage filesystem driver for Laravel 9 (using Flysystem v3 and its own GCS adapter).

Looking for Laravel 8 support? Use the v1 branch!

Support us

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.

Installation

You can install the package via composer:

composer require spatie/laravel-google-cloud-storage

Next, add a new disk to your filesystems.php config:

'gcs' => [
    'driver' => 'gcs',
    'key_file_path' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
    'key_file' => [], // optional: Array of data that substitutes the .json file (see below)
    'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'), // optional: is included in key file
    'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'),
    'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', ''), // optional: /default/path/to/apply/in/bucket
    'apiEndpoint' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below
    'visibility' => 'public', // optional: public|private
    'metadata' => ['cacheControl'=> 'public,max-age=86400'], // optional: default metadata
],

Usage

$disk = Storage::disk('gcs');

$disk->put('avatars/1', $fileContents);
$exists = $disk->exists('file.jpg');
$time = $disk->lastModified('file1.jpg');
$disk->copy('old/file1.jpg', 'new/file1.jpg');
$disk->move('old/file1.jpg', 'new/file1.jpg');
$url = $disk->url('folder/my_file.txt');
$url = $disk->temporaryUrl('folder/my_file.txt', now()->addMinutes(30));
$disk->setVisibility('folder/my_file.txt', 'public');

See https://laravel.com/docs/master/filesystem for full list of available functionality.

Authentication

The Google Client uses a few methods to determine how it should authenticate with the Google API.

  1. If you specify a path in the key key_file in disk config, that json credentials file will be used.

  2. If the GOOGLE_APPLICATION_CREDENTIALS env var is set, it will use that.

    putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
  3. 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
  4. If running in Google App Engine, the built-in service account associated with the application will be used.

  5. If running in Google Compute Engine, the built-in service account associated with the virtual machine instance will be used.

  6. If you want to authenticate directly without using a json file, you can specify an array for key_file in disk config with this data:

    'key_file' => [
        'type' => env('GOOGLE_CLOUD_ACCOUNT_TYPE'),
        'private_key_id' => env('GOOGLE_CLOUD_PRIVATE_KEY_ID'),
        'private_key' => env('GOOGLE_CLOUD_PRIVATE_KEY'),
        'client_email' => env('GOOGLE_CLOUD_CLIENT_EMAIL'),
        'client_id' => env('GOOGLE_CLOUD_CLIENT_ID'),
        'auth_uri' => env('GOOGLE_CLOUD_AUTH_URI'),
        'token_uri' => env('GOOGLE_CLOUD_TOKEN_URI'),
        'auth_provider_x509_cert_url' => env('GOOGLE_CLOUD_AUTH_PROVIDER_CERT_URL'),
        'client_x509_cert_url' => env('GOOGLE_CLOUD_CLIENT_CERT_URL'),
    ],

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 apiEndpoint 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.

Temporary / Signed URLs

With the latest adapter versions, you can easily generate a signed URLs for files that are not publicly visible by default.

$disk = Storage::disk('gcs');
$url = $disk->temporaryUrl('folder/my_file.txt', now()->addMinutes(30));
// https://storage.googleapis.com/test-bucket/folder/my_file.txt?GoogleAccessId=test-bucket%40test-gcp.iam.gserviceaccount.com&Expires=1571151576&Signature=tvxN1OS1txkWAUF0cCR3FWK%seRZXtFu42%04%YZACYL2zFQxA%uwdGEmdO1KgsHR3vBF%I9KaEzPbl4b7ic2IWUuo8Jh3IoZFqdTQec3KypjDtt%02DGwm%OO6pWDVV421Yp4z520%o5oMqGBtV8B3XmjW2PH76P3uID2QY%AlFxn23oE9PBoM2wXr8pDXhMPwZNJ0FtckSc26O8PmfVsG7Jvln%CQTU57IFyB7JnNxz5tQpc2hPTHbCGrcxVPEISvdOamW3I%83OsXr5raaYYBPcuumDnAmrK%cyS9%Ky2fL2B2shFO2cz%KRu79DBPqtnP2Zf1mJWBTwxVUCK2jxEEYcXBXtdOszIvlI6%tp2XfVwYxLNFU

Uniform bucket-level access

Google Cloud Storage allows setting permissions at the bucket level i.e. "Uniform bucket-level access".

Initially, the error "Cannot insert legacy ACL for an object when uniform bucket-level access is enabled" is observed.

When uploading files to such buckets ensure the visibility within the configuration file is set as follows:

use League\Flysystem\GoogleCloudStorage\PortableVisibilityHandler;
// ...
'visibility' => PortableVisibilityHandler::NO_PREDEFINED_VISIBILITY,

Please see https://cloud.google.com/storage/docs/access-control/signed-urls and https://laravel.com/docs/6.x/filesystem for more info.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].