You are here
Home > Php > Upload File to Google Cloud Storage using PHP

Upload File to Google Cloud Storage using PHP


Hello Friends, It’s being very long time and I haven’t shared anything. But today I am going to share how to upload file to Google Cloud Storage using PHP. With the help of this code we can upload files from third party hosting as well as from localhost (wamp, lamp, xampp, etc) to Google Cloud Storage. To upload files on Google Cloud Storage we are going to follow below steps :-

Step 1: Create Bucket
Step 2: Make Bucket Public
Step 3: Create Service Account & Download Key
Step 4: Install Composer & Download Google Cloud Storage Library
Step 5: Make Code Ready
Step 6: Upload File

Step 1: Create Bucket

First of all we have to create a bucket in which we will upload/store our files. Buckets are the basic containers that hold your data. Everything that you store in Cloud Storage must be contained in a bucket. To know more about bucket click buckets link in this paragraph. Below screenshot will help us to create bucket from GCP Console or we can execute “gsutil mb gs://[BUCKET_NAME]/” command in Cloud Shell.


Step 2: Make Bucket Public

After creating bucket our next step is to making it public so that we can access it’s object/files at third party hosting or at localhost using uri like “[BUCKET_NAME]/[OBJECT_NAME]”. Here also below screenshot will help us to make bucket public or we can make it public using “gsutil acl ch -u AllUsers:R gs://[BUCKET_NAME]/” command also.


after saving and making it public


Step 3: Create Service Account & Download Key

Our next step is to create service account and downloading private key so that we can use that private key to  connect to Google Cloud Storage. Below screenshot or command will help us to create service account and download private key.

step 1. creating service account



step 2. granting roles to service account



step 3. creating key




Step 4: Install Composer & Download Google Cloud Storage Library

Now in this step we’ll install Composer (Dependency Manager for PHP). After installing composer download Google Cloud Storage Library which we are going to use in our code and to download it, we’ll use Terminal (Command Line). In command line we’ll create new directory go inside it or we can go to our project folder if already created then we’ll run below command.

Above command will download only google/cloud-storage library because we want to keep our code small as possible so that we can easily upload our code on third party hosting if needed. Composer will download latest stable version of Google Cloud Storage library after executing above command. After download finishes will have a folder called ‘vendor’ containing library files, we don’t need to change anything inside it.

Step 5: Make Code Ready

Now our next step is to code. We will create three php files here config.php, requests.php, index.php and will upload file through ajax. Here config.php will contain private key and necessary functions, requests.php will use to handle ajax request to upload file and index.php for sending ajax request and receiving response.




Step 6: Upload File

Step 1
Step 2
Step 3

Note: –

Normally we can upload and download any type of file on Google Cloud Storage but if we want to load file using javascript – XMLHttpRequest (XHR) then by default GCP Storage don’t allow any domain to read file due to CORS policy, so to solve this problem we need create a configuration file to allow one or more domain to access files using XHR and to do that we’ll follow below steps:-

1. Check current configuration using below command

2. Create json file like below, containg all our domain name

3. Change current configuration using below command

Verifying CORS Settings

To verify CORS settings is working or not we’ll just open our website then we open chrome development console and use below ajax fetch command.

Before CORS Configuration

After CORS Configuration

NetBeans Project Download



Thank you Friends

Please don’t forget share if you like it

Rajesh Kumar Sahanee
I am a passionate Java Developer and I like Computer Programming. I love to do some interesting experiments and listening music in my free time.