- How do I integrate Google Cloud Storage with Laravel’s Filesystem for `Storage::disk('gcs')` support?
- Use the `league/flysystem-google-cloud-storage` adapter alongside `google/cloud-storage`. Configure it in Laravel’s `config/filesystems.php` under a new disk (e.g., `'gcs'`), then use `Storage::disk('gcs')->put()` like any other disk. The adapter bridges GCS operations to Laravel’s filesystem abstraction.
- What Laravel versions and PHP versions does `google/cloud-storage` support?
- The package supports PHP 8.1+ and is compatible with Laravel 10+. For older Laravel versions (e.g., 9.x), ensure PHP 8.0+ is used. Check the [Google Cloud PHP docs](https://github.com/googleapis/google-cloud-php) for version-specific notes, as minor updates may require adjustments.
- How do I authenticate `google/cloud-storage` in Laravel? Can I use service accounts?
- Yes, use service account credentials via Laravel’s config (e.g., `config('services.gcs.credentials')`). Store the JSON key file securely (e.g., Laravel Forge, Vault) and pass it to `StorageClient` during initialization. For dynamic auth, use Google’s [Application Default Credentials](https://github.com/googleapis/google-cloud-php/blob/main/AUTHENTICATION.md).
- Can I use the `gs://` stream wrapper in Laravel? Will it conflict with other stream handlers?
- Yes, register the stream wrapper via `$storage->registerStreamWrapper()`, but use it cautiously in production. It enables `file_get_contents('gs://bucket/file')` but may interfere with other stream wrappers (e.g., S3). For Laravel, prefer explicit `StorageClient` usage or wrap it in a service class to avoid global state issues.
- How do I handle large file uploads (e.g., videos >100MB) efficiently in Laravel?
- Use `StorageClient::upload()` with `resumable: true` for chunked uploads. For Laravel, wrap this in a job (e.g., `UploadFileJob`) and dispatch it via queues. Monitor progress with callbacks or GCS event notifications. Avoid memory issues by streaming files directly from disk or S3.
- Is there a way to cache GCS metadata locally to reduce API calls?
- Yes, cache object metadata (e.g., size, last-modified) in Redis or Laravel’s cache. Use `StorageClient::object()` to fetch fresh data when needed, but implement a TTL (e.g., 5 minutes) to avoid stale data. For critical apps, consider a hybrid cache with invalidation via GCS event triggers.
- How do I set up multi-tenancy with Google Cloud Storage in Laravel?
- Scope buckets by tenant (e.g., `tenant-{id}-media`) and instantiate a `StorageClient` per tenant in Laravel’s service container. Use IAM policies to enforce tenant-level permissions. For shared buckets, rely on object prefixes (e.g., `tenant1/files/`) and validate access in middleware or policies.
- What are the alternatives to `google/cloud-storage` for Laravel, and when should I use them?
- Alternatives include `league/flysystem-google-cloud-storage` (for Filesystem integration) or `aws-sdk/aws-sdk-php` (for S3). Use `google/cloud-storage` if you need direct GCS features (e.g., predefined ACLs, stream wrappers) or plan to use other Google Cloud services. For S3 compatibility, `league/flysystem-aws-s3` is a better fit.
- How do I monitor GCS usage and costs in a Laravel application?
- Use GCP’s [Cloud Monitoring](https://cloud.google.com/monitoring) to track storage, network, and API costs. In Laravel, log GCS operations (e.g., uploads/downloads) via a custom logging channel and set up alerts for anomalies. Implement lifecycle rules (e.g., auto-delete old files) via GCS console or Laravel’s scheduler.
- Can I use `google/cloud-storage` for Laravel queue jobs (e.g., storing failed jobs) or caching?
- Yes, configure the GCS queue driver in `config/queue.php` or use it as a cache driver (e.g., `cache:store --driver=gcs`). For failed jobs, store them in GCS via `queue:failed-table` with a custom GCS adapter. Note that GCS isn’t optimized for high-frequency writes like Redis, so benchmark performance for your use case.