Laravel Google Cloud Storage
Laravel, a popular PHP web application framework, offers built-in support for Google Cloud Storage (GCS) through its filesystem abstraction layer. This allows you to easily use GCS as a storage solution for your Laravel application, whether you need to store files, images, or any other assets.
To use Google Cloud Storage in a Laravel application, follow these steps:
Create a Google Cloud Storage Bucket:
- Sign in to your Google Cloud Console.
- Create a new project or select an existing one.
- Navigate to “Storage” > “Browser” and create a new bucket to store your files.
Install Required Packages: You will need to install the necessary packages to integrate GCS with Laravel. Use Composer to install them:
bashcomposer require google/cloud-storage
Configure GCS Credentials:
- Generate a JSON key file for your Google Cloud service account that has access to your GCS bucket.
- Store the JSON key file securely in your Laravel project (e.g., in the
storage
directory).
Configure Laravel to Use GCS: In your Laravel project, open the
config/filesystems.php
file and add a new disk configuration for Google Cloud Storage:php'gcs' => [ 'driver' => 'gcs', 'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'), 'key_file' => env('GOOGLE_CLOUD_KEY_FILE', 'path/to/your/keyfile.json'), 'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket-name'), 'path_prefix' => env('GOOGLE_CLOUD_PATH_PREFIX', null), 'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), ],
Set Environment Variables: In your
.env
file, set the necessary environment variables to match the configuration:makefileGOOGLE_CLOUD_PROJECT_ID=your-project-id GOOGLE_CLOUD_KEY_FILE=storage/path/to/your/keyfile.json GOOGLE_CLOUD_STORAGE_BUCKET=your-bucket-name
Use GCS in Your Laravel Application: You can now use the GCS disk in your Laravel code. For example, to store a file in GCS:
phpuse Illuminate\Support\Facades\Storage; Storage::disk('gcs')->put('path/to/your/file.jpg', $fileContents);
You can use the
Storage
facade to perform various filesystem operations on your GCS bucket.
By following these steps, you can integrate Google Cloud Storage seamlessly into your Laravel application, making it easy to store and manage your files in the cloud.
Google Cloud Training Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 IT Training Institute for Google Cloud Platform (GCP) Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Google Cloud Platform (GCP) here – Google Cloud Platform (GCP) Blogs
You can check out our Best In Class Google Cloud Platform (GCP) Training Details here – Google Cloud Platform (GCP) Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks