Laravel 9 google cloud Uploading image file

Laravel 9 google cloud Uploading image file

Laravel 9 google cloud Uploading image file

In this post we will give you information about Laravel 9 google cloud Uploading image file . Hear we will give you detail about Laravel 9 google cloud Uploading image file And how to use it also give you demo for it if it is necessary.

Google Cloud Storage for Laravel 9 google cloud Uploading image file

GCS (Google Cloud Storage) is an object storage in the cloud.

With GCS, I don’t need to care about how much space left in the server and images not available when GAE (Google App Engine) scales up the instances.

Prerequisites for Laravel 9 google cloud Uploading image file

Before you getting started, you need to prepare

  1. Create a project in the Google Cloud Platform Console.
  2. Install the Google Cloud SDK.
  3. Install Postman (this software is used for testing, optional to install)

Free GCS bucket for GAE

GCS provides a free bucket (allow to store up to 5GB) for each GAE application. You can a bucket created with your application domain name under GCS browser.

Laravel 9 google cloud Uploading image file

Install GCS in Laravel

Next, install GCS as a custom file system in your Laravel 9 application. Superbalist released a handy package as a GCS adapter for flysystem.

The first step, install the package.

// Terminal

$ composer require superbalist/laravel-google-cloud-storage

Register the service provider.

// config/app.php

'providers' => [


         * Package Service Providers...



Next step, add a new disk to the filesystem.

// config/filesystems.php

'disks' => [


        'gcs' => [
            'driver' => 'gcs',
            'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
            'key_file' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
            'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'),
            'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket
            'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below

There are 5 environment variables you can set:

  1. GOOGLE_CLOUD_PROJECT_ID – your GCP project ID
  2. GOOGLE_CLOUD_KEY_FILE – your service account, you don’t need to set this now because GAE has built-in credential ready. Check out more from here
  3. GOOGLE_CLOUD_STORAGE_BUCKET – your bucket name, in my case, it is
  4. GOOGLE_CLOUD_STORAGE_PATH_PREFIX – your image path/folder in the bucket
  5. GOOGLE_CLOUD_STORAGE_API_URI – your custom domain, you don’t need to set this now. Check out more from here

The last step in the installation, set the variables in app.yaml.

// app.yaml

# Put production environment variables here.
  GOOGLE_CLOUD_PROJECT_ID: running-laravel-on-gcp

Once the installation is done, you create an endpoint for image uploading.

Create an endpoint for image upload

Create a new controller named UserAvatarController.

// Terminal

$ php artisan make:controller UserAvatarController

In UserAvatarController, create a method named update which stores the uploaded image file to GCS bucket into the path/folder named avatars.

// app/Http/Controllers/UserAvatarController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserAvatarController extends Controller
     * Update the avatar for the user.
     * @param  Request  $request
     * @return Response
    public function update(Request $request)
        $path = $request->file('avatar')->store('avatars');

        return $path;

Don’t forget to enable the endpoint to the route file.

// routes/web.php

Route::post('/updateAvatar', 'UserAvatarController@update’);

In this case, you aren’t going to build a form to test the upload. If you are, you can skip this step.

The upload test will be running via Postman which cannot generate the CSRF token automatically. Therefore, you exclude the /updateAvatar from the VerifyCsrfToken Middleware.

// app/Http/Middleware/VerifyCsrfToken.php

class VerifyCsrfToken extends Middleware

     * The URIs that should be excluded from CSRF verification.
     * @var array
    protected $except = [


Deploy the application to the GAE.

// Terminal

$ gcloud app deploy

Test forLaravel 9 google cloud Uploading image file

Once the deployment is completed, run the test now.

You can use Postman or another method to test the application. Now, you are going to use Postman.

From the Postman,

  1. Create a new POST request
  2. Set the domain and the endpoint
  3. Name a field data as avatar and set the type as File
  4. Select an image
  5. Click Send

After clicking the send button, the endpoint should return the file path and the uploaded image file name.


Laravel 9 google cloud Uploading image file

You can find the uploaded image file through GCP console.

Laravel 9 google cloud Uploading image file

Conclusion for Laravel 9 google cloud Uploading image file

Hope this code and post will helped you for implement Laravel 9 Yajra Datatables Example Tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us.

For More Info See :: laravel And github

Leave a Comment

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

13  +    =  16

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US