• Stars
    star
    508
  • Rank 86,941 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Google Cloud Storage filesystem for Laravel

laravel-google-cloud-storage

A Google Cloud Storage filesystem for Laravel.

Author Build Status Software License Packagist Version Total Downloads

This package is a wrapper bridging flysystem-google-storage into Laravel as an available storage disk.

Installation

composer require superbalist/laravel-google-cloud-storage

If you are on Laravel 5.4 or earlier, then register the service provider in app.php

'providers' => [
    // ...
    Superbalist\LaravelGoogleCloudStorage\GoogleCloudStorageServiceProvider::class,
]

If you are on Laravel 5.5 or higher, composer will have registered the provider automatically for you.

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.

  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 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');

// Set the visibility of file to public
$disk->setVisibility('folder/my_file.txt', 'public');


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

More Repositories

1

flysystem-google-cloud-storage

Flysystem Adapter for Google Cloud Storage
PHP
266
star
2

laravel-prometheus-exporter

A prometheus exporter for Laravel
PHP
155
star
3

laravel-pubsub

A Pub-Sub abstraction for Laravel
PHP
68
star
4

php-pubsub

A PHP abstraction for the pub-sub pattern
PHP
45
star
5

php-pubsub-kafka

A Kafka adapter for the php-pubsub package
PHP
30
star
6

php-pubsub-redis

A Redis adapter for the php-pubsub package
PHP
24
star
7

monolog-google-cloud-json-formatter

A Monolog extension for formatting log entries for Google Cloud Logging
PHP
15
star
8

php-pubsub-google-cloud

A Google Cloud adapter for the php-pubsub package
PHP
12
star
9

php-money

A money and currency library for handling arbitrary-precision arithmetic
PHP
11
star
10

laravel-zendesk

A Laravel Zendesk library for integrating with the Zendesk API
PHP
9
star
11

php-event-pubsub

An event protocol and implementation over pub/sub
PHP
8
star
12

python-pubsub

Python
6
star
13

laravel-event-pubsub

An event protocol and implementation over pub/sub for Laravel
PHP
6
star
14

laravel4-storage

A filesystem abstraction library for Laravel 4
PHP
4
star
15

laravel-ajax-redirector

A Laravel library for handling AJAX redirects
PHP
3
star
16

js-pubsub

A JS abstraction for the pub-sub pattern
JavaScript
3
star
17

js-pubsub-google-cloud

A Google Cloud adapter for the js-pubsub package
JavaScript
2
star
18

laravel-lusitanian-oauth-session-store

A Laravel session storage interface for the lusitanian/oauth library
PHP
2
star
19

js-pubsub-redis

A Redis adapter for the js-pubsub package
JavaScript
2
star
20

js-event-pubsub

An event protocol and implementation over pub/sub
JavaScript
2
star
21

js-pubsub-http

An HTTP adapter for the js-pubsub package
JavaScript
2
star
22

laravel-appboy

A Laravel library for sending push notifications via the Appboy API
PHP
1
star
23

simple-sms-panacea-mobile

An adapter for the simple-sms Laravel library for sending SMSs via the Panacea Mobile API
PHP
1
star
24

laravel4-event-pubsub

An event protocol and implementation over pub/sub for Laravel 4
PHP
1
star
25

panaceaapi

The Panacea Mobile PHP class for easy deployment and integration
PHP
1
star
26

php-panaceamobile

An API client for sending SMSs via the Panacea Mobile API
PHP
1
star
27

spreeza-dust-recursive-loader

Webpack loader for processing dust templates recursively.
JavaScript
1
star
28

laravel4-psr6-cache-bridge

A PSR6 cache implementation for Laravel 4
PHP
1
star
29

js-pubsub-manager

A manager & factory for the js-pubsub package
JavaScript
1
star
30

php-pubsub-http

An HTTP adapter for the php-pubsub package
PHP
1
star
31

lint-review-images

Images built for use with lint-review
Dockerfile
1
star
32

laravel4-pubsub

A Pub-Sub abstraction for Laravel 4
PHP
1
star
33

php-appboy

A PHP client for sending push notifications via the Appboy API
PHP
1
star
34

js-pubsub-rest-proxy

An HTTP server which acts as a gateway for publishing messages via a js-pubsub adapter
JavaScript
1
star