How to integrate Google Cloud Storage with Laravel

Lokman Musliu Founder and CEO of Lucky Media
Lokman Musliu

July 7, 2025 · 4 min read

How to integrate Google Cloud Storage with Laravel

In this post, we'll walk you through integrating Google Cloud Storage (GCS) into your Laravel application, enabling you to upload, store, and serve files at scale. Whether you're building media-rich applications or need reliable, scalable cloud storage, this step-by-step guide will help you set up everything, from Google Cloud Storage configuration to Laravel usage. We will use the popular Spatie Google Cloud Storage package.

Using Google Cloud Storage with Laravel

Google Cloud Storage is reliable, scalable, and provides a robust solution for storing and accessing data. We recently used it for a project and decided to document the process in a blog post. Let's start by setting up our Google Cloud Platform environment.

Configuring Google Cloud Storage

Configuring Google Cloud Storage

Step 1. Create a New Project

Google Cloud Storage Dashboard
  1. Navigate to the GCP dashboard.

  2. Click on "Select a Project" at the top and then "New Project".

  3. Choose a meaningful project name related to your app to avoid confusion if managing multiple projects. (e.g., 'filament-cloud-project').

  4. After creation, select the new project. This ensures all subsequent actions are applied to the right context.

Step 2. Create a GCS Service Account

Google Cloud Storage - Service Accounts

Once the project is set up, it's time to create a service account. You can create different service accounts for different environments.

  1. Go to Google Cloud Console > IAM & Admin > Service Accounts.

  2. Click “Create Service Account.”

  3. Give it a memorable name (e.g., 'filament-project-cloud-service').

  4. Click “Create and Continue.”

  5. Assign Roles to the Service Account

    - Assign roles: Owner, Storage Admin, Storage Object Admin, and Storage Folder Admin. This gives the service account the right permissions to manage the storage needs.

  6. Grant Access

    - Add principals who need to perform actions using this account. This could be other team members or applications.

Google Cloud Storage - Permissions

Step 3. Create a GCS Bucket

Google Cloud Storage - Bucket

Buckets are the storage containers of GCS. You can make buckets public (when serving public files like images) or keep them private (recommended). If private, you can generate signed URLs in Laravel to grant temporary access. For simplicity, we will create a public bucket.

  1. Go to Cloud Storage > Buckets.

  2. Click "Create Bucket". Choose a globally unique name (e.g., 'filament-cloud-bucket').

  3. Configure the Bucket.

    1. Region: Choose based on location/performance needs.

    2. Default Storage Class: Set it to "Standard."

    3. Access Control: Uncheck "Enforce public access prevention on this bucket."

Google Cloud Storage Bucket Region

Step 4. Generate a JSON Key for the Service Account

Create new Google Cloud Storage key

Laravel uses this key to authenticate API requests to Google Cloud Storage.

  1. Back in the Service Account, open the Keys tab.

  2. Choose "Add Key > Create New Key".

  3. Choose JSON and download the key.

  4. Move this JSON file to the Laravel project root folder.

Setting Up Laravel for Google Cloud Storage

Now, let’s get our Laravel environment communicating with GCS.

Step 1. Install the Spatie Google Cloud Storage Package

Run the following composer command to install the Spatie Google Cloud Storage package:

composer require spatie/laravel-google-cloud-storage

Step 2. Configure the Filesystem

Open config/filesystems.php and add a new disk configuration in the disks array:

'gcs' => [
    'driver' => 'gcs',
    'key_file_path' => base_path(env('GOOGLE_CLOUD_KEY_FILE', 'your-json-file.json')),
    'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
    'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket-id'),
    'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', ''),
    'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null),
    'apiEndpoint' => env('GOOGLE_CLOUD_STORAGE_API_ENDPOINT', null),
    'visibility' => 'public',
    'visibility_handler' => \League\Flysystem\GoogleCloudStorage\UniformBucketLevelAccessVisibility::class,
    'metadata' => ['cacheControl'=> 'public,max-age=86400'],
]

Step 3. Add Environment variables

Update the .env file with the following:

GOOGLE_CLOUD_PROJECT_ID=your_project_id
GOOGLE_CLOUD_STORAGE_BUCKET=your_bucket_id
GOOGLE_CLOUD_KEY_FILE=your_json_file_name.json

File Management with Laravel and Google Cloud Storage

With everything set up, you can upload and download files using Laravel’s Storage facade. Here’s how:

Uploading files to Google Cloud Storage with Laravel

We can now easily download files from Google Cloud Storage with the Laravel Storage facade:

Storage::disk('gcs')->put('folder/file.txt', 'Contents here...');

Downloading files to Google Cloud Storage with Laravel

Downloading files from Google Cloud Storage can also be done with the Laravel Storage facade:

Storage::disk('gcs')->download('folder/file.txt');

Conclusion

In this article, we have integrated Google Cloud Storage into a Laravel application using FilamentPHP. We've built a scalable, reliable solution for managing files. Whether you're building the next big app or just tinkering around, Google Cloud Storage provides a great solution for your storage needs.


Bring Your Ideas to Life 🚀

If you need help with a Laravel project let’s get in touch.

Lucky Media is proud to be recognized as a leading Laravel Development Agency

FAQs

Why should I use Google Cloud Storage over other cloud storage services?

Google Cloud Storage offers a robust, scalable, and cost-effective solution with seamless integration into the Google Cloud ecosystem, making it a great choice for developers already using other Google services.

What are the benefits of using Google Cloud Storage with Laravel?

Using Google Cloud Storage with Laravel offers benefits such as scalability, reliability, and easy integration with other Google Cloud services. It also provides robust security features and global access to your data.

What are the costs associated with Google Cloud Storage?

Google Cloud Storage pricing varies based on storage class, data location, and operations performed. It's best to check Google's pricing page for the most current information.

How secure is Google Cloud Storage?

Google Cloud Storage provides robust security features, including encryption at rest and in transit, access control policies, and more, ensuring your data is well-protected.

Can I use Google Cloud Storage for storing large files?

Yes, Google Cloud Storage is ideal for storing large files due to its scalability and high availability. You can easily manage large datasets without worrying about storage limitations.

How can I keep my GCS files private while still providing access to users?

Use signed (temporary) URLs generated by Laravel’s Storage::disk('gcs')->temporaryUrl() method. These URLs grant time-limited access without making your bucket public.

Technologies

Laravel
Lokman Musliu Founder and CEO of Lucky Media
Lokman Musliu

Founder and CEO of Lucky Media

Stay up-to-date

Be updated with all news, products and tips we share!

Let’s chat

We partner with a limited number of brands each quarter to ensure senior-level attention on every project.

lokman and arlind headshots
Teamwork