Laravel Google Cloud Storage

Share

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:

  1. 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.
  2. Install Required Packages: You will need to install the necessary packages to integrate GCS with Laravel. Use Composer to install them:

    bash
    composer require google/cloud-storage
  3. 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).
  4. 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), ],
  5. Set Environment Variables: In your .env file, set the necessary environment variables to match the configuration:

    makefile
    GOOGLE_CLOUD_PROJECT_ID=your-project-id GOOGLE_CLOUD_KEY_FILE=storage/path/to/your/keyfile.json GOOGLE_CLOUD_STORAGE_BUCKET=your-bucket-name
  6. 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:

    php
    use 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:

You can find more information about Google Cloud in this Google Cloud Link

 

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


Share

Leave a Reply

Your email address will not be published. Required fields are marked *