1
- # : package_description
1
+ # Google Cloud Storage filesystem driver for Laravel
2
2
3
- [ ![ Latest Version on Packagist] ( https://img.shields.io/packagist/v/vendor_slug/package_slug .svg?style=flat-square )] ( https://packagist.org/packages/vendor_slug/package_slug )
4
- [ ![ GitHub Tests Action Status] ( https://img.shields.io/github/workflow/status/vendor_slug/package_slug/run-tests ?label=tests )] ( https://github.com/vendor_slug/package_slug /actions?query=workflow%3Arun-tests+branch%3Amain )
5
- [ ![ GitHub Code Style Action Status] ( https://img.shields.io/github/workflow/status/vendor_slug/package_slug /Check%20&%20fix%20styling?label=code%20style )] ( https://github.com/vendor_slug/package_slug /actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain )
6
- [ ![ Total Downloads] ( https://img.shields.io/packagist/dt/vendor_slug/package_slug .svg?style=flat-square )] ( https://packagist.org/packages/vendor_slug/package_slug )
3
+ [ ![ Latest Version on Packagist] ( https://img.shields.io/packagist/v/spatie/laravel-google-cloud-storage .svg?style=flat-square )] ( https://packagist.org/packages/spatie/laravel-google-cloud-storage )
4
+ [ ![ GitHub Tests Action Status] ( https://img.shields.io/github/workflow/status/spatie/laravel-google-cloud-storage/Tests/main ?label=tests )] ( https://github.com/spatie/laravel-google-cloud-storage /actions?query=workflow%3Arun-tests+branch%3Amain )
5
+ [ ![ GitHub Code Style Action Status] ( https://img.shields.io/github/workflow/status/spatie/laravel-google-cloud-storage /Check%20&%20fix%20styling/main ?label=code%20style )] ( https://github.com/spatie/laravel-google-cloud-storage /actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain )
6
+ [ ![ Total Downloads] ( https://img.shields.io/packagist/dt/spatie/laravel-google-cloud-storage .svg?style=flat-square )] ( https://packagist.org/packages/spatie/laravel-google-cloud-storage )
7
7
8
8
---
9
- This repo can be used to scaffold a Laravel package. Follow these steps to get started:
10
9
11
- 1 . Press the "Use template" button at the top of this repo to create a new repo with the contents of this skeleton
12
- 2 . Run "./configure-skeleton.sh" to run a script that will replace all placeholders throughout all the files
13
- 3 . Remove this block of text.
14
- 4 . Have fun creating your package.
15
- 5 . If you need help creating a package, consider picking up our <a href =" https://laravelpackage.training " >Laravel Package Training</a > video course.
16
- ---
17
-
18
- This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
10
+ Google Cloud Storage filesystem driver for Laravel
19
11
20
12
## Support us
21
13
22
- [ <img src =" https://github-ads.s3.eu-central-1.amazonaws.com/:package_name .jpg?t=1 " width =" 419px " />] ( https://spatie.be/github-ad-click/:package_name )
14
+ [ <img src =" https://github-ads.s3.eu-central-1.amazonaws.com/laravel-google-cloud-storage .jpg?t=1 " width =" 419px " />] ( https://spatie.be/github-ad-click/laravel-google-cloud-storage )
23
15
24
16
We invest a lot of resources into creating [ best in class open source packages] ( https://spatie.be/open-source ) . You can support us by [ buying one of our paid products] ( https://spatie.be/open-source/support-us ) .
25
17
@@ -30,35 +22,113 @@ We highly appreciate you sending us a postcard from your hometown, mentioning wh
30
22
You can install the package via composer:
31
23
32
24
``` bash
33
- composer require vendor_slug/package_slug
25
+ composer require spatie/laravel-google-cloud-storage
34
26
```
35
27
36
- You can publish and run the migrations with :
28
+ Next, add a new disk to your ` filesystems.php ` config :
37
29
38
- ``` bash
39
- php artisan vendor:publish --provider=" VendorName\Skeleton\SkeletonServiceProvider" --tag=" package_slug-migrations"
40
- php artisan migrate
30
+ ``` php
31
+ 'gcs' => [
32
+ 'driver' => 'gcs',
33
+ 'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
34
+ 'key_file_path' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
35
+ 'key_file' => [], // optional: Array of data that substitutes the .json file (see below)
36
+ 'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'),
37
+ 'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket
38
+ 'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below
39
+ 'visibility' => 'public', // optional: public|private
40
+ 'metadata' => ['cacheControl'=> 'public,max-age=86400'], // optional: default metadata
41
+ ],
41
42
```
42
43
43
- You can publish the config file with:
44
- ``` bash
45
- php artisan vendor:publish --provider=" VendorName\Skeleton\SkeletonServiceProvider" --tag=" package_slug-config"
44
+ ## Usage
45
+
46
+ ``` php
47
+ $disk = Storage::disk('gcs');
48
+
49
+ $disk->put('avatars/1', $fileContents);
50
+ $exists = $disk->exists('file.jpg');
51
+ $time = $disk->lastModified('file1.jpg');
52
+ $disk->copy('old/file1.jpg', 'new/file1.jpg');
53
+ $disk->move('old/file1.jpg', 'new/file1.jpg');
54
+ $url = $disk->url('folder/my_file.txt');
55
+ $url = $disk->temporaryUrl('folder/my_file.txt', now()->addMinutes(30));
56
+ $disk->setVisibility('folder/my_file.txt', 'public');
46
57
```
47
58
48
- This is the contents of the published config file:
59
+ See https://laravel.com/docs/master/filesystem for full list of available functionality.
60
+
61
+ ### Authentication
62
+
63
+ The Google Client uses a few methods to determine how it should authenticate with the Google API.
64
+
65
+ 1 . If you specify a path in the key ` key_file ` in disk config, that json credentials file will be used.
66
+ 2 . If the ` GOOGLE_APPLICATION_CREDENTIALS ` env var is set, it will use that.
67
+ ``` php
68
+ putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
69
+ ```
70
+ 3 . It will then try load the key file from a 'well known path':
71
+ * windows: %APPDATA%/gcloud/application_default_credentials.json
72
+ * others: $HOME/.config/gcloud/application_default_credentials.json
73
+
74
+ 4 . If running in ** Google App Engine** , the built-in service account associated with the application will be used.
75
+ 5 . If running in ** Google Compute Engine** , the built-in service account associated with the virtual machine instance will be used.
76
+ 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:
77
+ ``` php
78
+ 'key_file' => [
79
+ 'type' => env('GOOGLE_CLOUD_ACCOUNT_TYPE'),
80
+ 'private_key_id' => env('GOOGLE_CLOUD_PRIVATE_KEY_ID'),
81
+ 'private_key' => env('GOOGLE_CLOUD_PRIVATE_KEY'),
82
+ 'client_email' => env('GOOGLE_CLOUD_CLIENT_EMAIL'),
83
+ 'client_id' => env('GOOGLE_CLOUD_CLIENT_ID'),
84
+ 'auth_uri' => env('GOOGLE_CLOUD_AUTH_URI'),
85
+ 'token_uri' => env('GOOGLE_CLOUD_TOKEN_URI'),
86
+ 'auth_provider_x509_cert_url' => env('GOOGLE_CLOUD_AUTH_PROVIDER_CERT_URL'),
87
+ 'client_x509_cert_url' => env('GOOGLE_CLOUD_CLIENT_CERT_URL'),
88
+ ],
89
+ ```
90
+
91
+ ### Public URLs
92
+
93
+ The adapter implements a `getUrl($path)` method which returns a public url to a file.
94
+ >**Note:** Method available for Laravel 5.2 and higher. If used on 5.1, it will throw an exception.
49
95
50
96
```php
51
- return [
52
- ];
97
+ $disk = Storage::disk('gcs');
98
+ $url = $disk->url('folder/my_file.txt');
99
+ // http://storage.googleapis.com/bucket-name/folder/my_file.txt
53
100
```
54
101
55
- ## Usage
102
+ If you configure a ` path_prefix ` in your config:
103
+ ``` php
104
+ $disk = Storage::disk('gcs');
105
+ $url = $disk->url('folder/my_file.txt');
106
+ // http://storage.googleapis.com/bucket-name/path-prefix/folder/my_file.txt
107
+ ```
56
108
109
+ If you configure a custom ` storage_api_uri ` in your config:
57
110
``` php
58
- $skeleton = new VendorName\Skeleton();
59
- echo $skeleton->echoPhrase('Hello, Spatie!');
111
+ $disk = Storage::disk('gcs');
112
+ $url = $disk->url('folder/my_file.txt');
113
+ // http://your-custom-domain.com/bucket-name/path-prefix/folder/my_file.txt
60
114
```
61
115
116
+ For a custom domain (storage api uri), you will need to configure a CNAME DNS entry pointing to ` storage.googleapis.com ` .
117
+
118
+ Please see https://cloud.google.com/storage/docs/xml-api/reference-uris#cname for further instructions.
119
+
120
+ ### Temporary / Signed URLs
121
+
122
+ With the latest adapter versions, you can easily generate a signed URLs for files that are not publicly visible by default.
123
+
124
+ ``` php
125
+ $disk = Storage::disk('gcs');
126
+ $url = $disk->temporaryUrl('folder/my_file.txt', now()->addMinutes(30));
127
+ // 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
128
+ ```
129
+
130
+ Please see https://cloud.google.com/storage/docs/access-control/signed-urls and https://laravel.com/docs/6.x/filesystem for more info.
131
+
62
132
## Testing
63
133
64
134
``` bash
@@ -79,7 +149,7 @@ Please review [our security policy](../../security/policy) on how to report secu
79
149
80
150
## Credits
81
151
82
- - [ : author_name ] ( https://github.com/:author_username )
152
+ - [ Alex Vanderbist ] ( https://github.com/alexvanderbist )
83
153
- [ All Contributors] ( ../../contributors )
84
154
85
155
## License
0 commit comments