- How do I integrate Google Cloud Storage with Laravel’s Filesystem (e.g., replace `storage/app`)?
- Use a custom Filesystem adapter like `google/cloud-storage-adapter` (third-party) or wrap the `StorageClient` in a `Filesystem` contract. Bind it in Laravel’s `config/filesystems.php` under a new disk (e.g., `google`) and reference it via `Storage::disk('google')->put()`. The package’s `gs://` stream wrapper also enables direct file operations like `file_get_contents('gs://bucket/file')`.
- What Laravel versions officially support `google/cloud-storage`?
- The package supports PHP 8.1–8.4, which aligns with Laravel 9.x–11.x. For older Laravel versions (e.g., 8.x), ensure your PHP version meets the package’s requirements (8.1+). Use `composer require google/cloud-storage` and Laravel’s `php-version` constraint in `composer.json` to enforce compatibility.
- How do I handle authentication in Laravel (e.g., service account keys securely)?
- Store your Google Cloud service account JSON key file path in Laravel’s `.env` (e.g., `GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json`). Initialize the `StorageClient` with `putenv()` or pass the path directly: `$storage = new StorageClient(['keyFilePath' => env('GOOGLE_APPLICATION_CREDENTIALS')])`. Restrict IAM roles to least privilege and avoid hardcoding credentials.
- Can I use Cloud Storage for Laravel queue jobs (e.g., processing uploaded files)?
- Yes. Trigger Cloud Storage events (e.g., `OBJECT_FINALIZE`) to push messages to Google Pub/Sub, then consume them in Laravel queues. Alternatively, poll for new objects in a Laravel job using `StorageClient::bucket()->objects()` with a `where()` filter for metadata (e.g., `uploaded_by = 'laravel'`).
- What’s the best way to optimize costs for high-volume Laravel uploads (e.g., user-generated media)?
- Use lifecycle rules to transition objects to cheaper classes (e.g., Nearline after 30 days) via Google Cloud Console. Enable object versioning to avoid duplicate storage costs. For Laravel, implement a `beforeUpload` event to resize/compress images before uploading. Monitor usage with the Cloud Billing API and set budget alerts.
- How do I generate signed URLs for secure file downloads in Laravel?
- Use the `StorageClient`’s `object()->signedUrl()` method with a short expiry (e.g., 15 minutes): `$url = $object->signedUrl(['expiration' => time() + 900]);`. Integrate this with Laravel’s auth middleware (e.g., `can:download-file`) to validate permissions before generating URLs. Cache signed URLs in Redis if needed.
- Will this package work with Laravel Forge or Valet for local development?
- Yes, but configure authentication via `GOOGLE_APPLICATION_CREDENTIALS` in `.env`. For Forge, use the server’s service account key. For Valet, set up ADC (Application Default Credentials) by running `gcloud auth application-default login` locally. Test with the `gs://` stream wrapper or direct `StorageClient` calls.
- Are there alternatives to `google/cloud-storage` for Laravel if I need multi-cloud support?
- For multi-cloud, consider `league/flysystem-google` (Google-specific) or `league/flysystem` with adapters like `spatie/flysystem-dropbox`/`spatie/flysystem-aws`. However, these lack Google’s native features (e.g., resumable uploads, event-driven workflows). For hybrid setups, use `spatie/laravel-medialibrary` with a custom Google adapter.
- How do I handle large file uploads (>5GB) in Laravel without timeouts?
- Use the `StorageClient`’s resumable uploads for files >5MB: `$bucket->upload($file, ['resumable' => true])`. For Laravel, chunk files client-side (e.g., with JavaScript) and reassemble in a queue job. Monitor progress via the `upload()` callback. Ensure your Google Cloud project has quotas for large objects enabled.
- Can I use Cloud Storage as a fallback for Laravel’s local storage when offline?
- Not natively, but implement a hybrid strategy: use `spatie/laravel-medialibrary` with a local filesystem fallback, then sync to Google Cloud via a Laravel queue job when online. Alternatively, use `league/flysystem` with a `Cache` adapter for offline writes and a `Google` adapter for persistence. Test with `Storage::fake()` for local mocking.