spatie/laravel-google-cloud-storage
Laravel 9+ Google Cloud Storage filesystem driver using Flysystem v3 and a dedicated GCS adapter. Adds a gcs disk with service account key file/array support, project and bucket config, path prefixes, endpoints, and public/private visibility options.
Installation:
composer require spatie/laravel-google-cloud-storage
Publish the config file:
php artisan vendor:publish --provider="Spatie\GoogleCloudStorage\GoogleCloudStorageServiceProvider" --tag="google-cloud-storage-config"
Configuration:
.env:
GOOGLE_CLOUD_STORAGE_KEY_FILE=/path/to/your/service-account-key.json
GOOGLE_CLOUD_STORAGE_BUCKET=your-bucket-name
config/filesystems.php:
'disks' => [
'gcs' => [
'driver' => 'google',
'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET'),
'project_id' => env('GOOGLE_CLOUD_STORAGE_PROJECT_ID', 'your-project-id'),
'key_file' => env('GOOGLE_CLOUD_STORAGE_KEY_FILE'),
'root' => env('GOOGLE_CLOUD_STORAGE_ROOT', ''),
'throw' => env('GOOGLE_CLOUD_STORAGE_THROW', false),
],
],
First Use Case:
Upload a file using Laravel’s built-in Storage facade:
use Illuminate\Support\Facades\Storage;
Storage::disk('gcs')->put('path/to/file.txt', 'file contents');
File Operations:
Storage facade for consistency:
// Upload
Storage::disk('gcs')->put('file.jpg', file_get_contents('local.jpg'));
// Download
$contents = Storage::disk('gcs')->get('file.jpg');
// Delete
Storage::disk('gcs')->delete('file.jpg');
Directory Handling:
Storage::disk('gcs')->makeDirectory('folders/subfolder');
$files = Storage::disk('gcs')->files('folder/');
URL Generation:
$url = Storage::disk('gcs')->url('file.jpg');
Temporary Files:
temporaryUrl() for time-limited access:
$url = Storage::disk('gcs')->temporaryUrl('file.jpg', now()->addMinutes(15));
Storage facade for seamless integration with existing code (e.g., Storage::put(), Storage::disk()).spatie/laravel-medialibrary or intervention/image:
$image = Image::make('local.jpg')->resize(300, 200);
Storage::disk('gcs')->put('resized.jpg', $image->stream());
dispatch(new UploadToGCSJob($filePath, 'gcs'));
config() helper to dynamically switch buckets:
$disk = config('app.env') === 'production' ? 'gcs_prod' : 'gcs_dev';
Storage::disk($disk)->put(...);
Permission Issues:
Storage Admin or Storage Object Admin roles in Google Cloud IAM.GOOGLE_CLOUD_STORAGE_THROW=true in .env to surface permission errors.Bucket Naming:
.env.CORS Configuration:
curl -I https://storage.googleapis.com/your-bucket/file.jpg
Large File Uploads:
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient();
$bucket = $storage->bucket('your-bucket');
$object = $bucket->upload(
fopen('large-file.zip', 'r'),
['name' => 'large-file.zip']
);
Timeouts:
Storage::disk()->listContents() with pagination:
$objects = Storage::disk('gcs')->listContents('folder/', false);
GOOGLE_CLOUD_STORAGE_THROW=true to convert exceptions to HTTP errors.config/google-cloud-storage.php:
'logging' => [
'enabled' => env('GOOGLE_CLOUD_STORAGE_LOGGING', false),
'level' => env('GOOGLE_CLOUD_STORAGE_LOG_LEVEL', 'info'),
],
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient(['logging' => ['debug' => true]]);
Custom Metadata: Add metadata during upload:
Storage::disk('gcs')->put('file.jpg', $contents, [
'metadata' => [
'customKey' => 'customValue',
'contentType' => 'image/jpeg',
],
]);
Event Listeners:
Listen to file system events (e.g., Storage::disk('gcs')->put()) via Laravel’s Storage events:
Storage::disk('gcs')->put('file.jpg', $contents);
// Triggered via event: 'storage:file:created'
Flysystem Adapter: Access the underlying Flysystem adapter for advanced operations:
$adapter = Storage::disk('gcs')->getAdapter();
$adapter->writeStream('file.txt', fopen('local.txt', 'r'));
Retry Logic: Implement custom retry logic for transient failures:
use Spatie\GoogleCloudStorage\GoogleCloudStorage;
$gcs = new GoogleCloudStorage();
$gcs->withRetry(3, 100)->put(...);
Local Testing:
Use google-cloud-storage’s local emulator for testing:
docker run -p 4443:4443 gcr.io/google.com/cloudsdktool/google-cloud-cli
Configure .env to point to the emulator:
GOOGLE_CLOUD_STORAGE_EMULATOR_HOST=localhost:4443
How can I help you explore Laravel packages today?