How to upload files to Google cloud storage from a Laravel application

  • File organization
  • Easily optimized
  • Makes application faster
  • Makes team collaboration easier
  • Minimize the risk of loosing media files
laravel new laravel_upload_using_google_cloud_storage
composer create-project laravel/laravel_upload_using_google_cloud_storage
cd laravel_upload_using_google_cloud_storage
DB_DATABASE=laravel_upload_using_google_cloud_storage DB_USERNAME=root
php artisan make:controller UploadController
public function upload()
public function store(Request $request)
Route::middleware(['auth'])->group(function () {
Route::get('/home', [UploadController::class, 'upload'])->name('home');
Route::post('uploads/store', [UploadController::class, 'store'])->name('');
Project info 
Project name Laravel upload
Project number 1549378xxxxx
Project ID laravel-upload-98xxxx
GOOGLE_CLOUD_PROJECT_ID=laravel_upload_using_google_cloud_storage GOOGLE_CLOUD_STORAGE_BUCKET=laravel-upload-gcs 
return [ 'project_id' => env('GOOGLE_CLOUD_PROJECT_ID'), 'storage_bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET'), ]; 
composer require google/cloud-storage
composer dump-autoload 
php artisan optimize:clear
use Google\Cloud\Storage\StorageClient; 
public function store(Request $request)
//validate the file upload
'avatar' => 'required|image|max:10240',
//get the authenticated user
$user = Auth::user();
//get the credentials in the json file
$googleConfigFile = file_get_contents(config_path('laravel-project.json'));
//create a StorageClient object
$storage = new StorageClient([
'keyFile' => json_decode($googleConfigFile, true)
//get the bucket name from the env file
$storageBucketName = config('googlecloud.storage_bucket');
//pass in the bucket name
$bucket = $storage->bucket($storageBucketName);
$avatar_request = $request->file('avatar');$image_path = $avatar_request->getRealPath();//rename the file
$avatar_name = $user->name.'-'.time().'.'.$avatar_request->extension();
//open the file using fopen
$fileSource = fopen($image_path, 'r');
//specify the path to the folder and sub-folder where needed
$googleCloudStoragePath = 'laravel-upload/' . $avatar_name;
//Delete previously uploaded image to cloud storage by this user
if(Auth::user()->avatar !== ''){
$object = $bucket->object('laravel-upload/'.Auth::user()->avatar );
//upload the new file to google cloud storage
$bucket->upload($fileSource, [
'predefinedAcl' => 'publicRead',
'name' => $googleCloudStoragePath
//pass in the name of the file and save
$user->avatar = $avatar_name ;
//return a success message to the user
return redirect()->route('home')
->with('success','Uploaded successfully.');
//laravel 9
public function avatar(): Attribute
return new Attribute(
get: fn ($value) => ''.$value,



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alemoh Rapheal B. Enike

Alemoh Rapheal B. Enike

Software developer |writer | librarian | (Tech is easy to Learn)