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

Upload File to Google Cloud Storage using PHP

upload-file-google-cloud-storage-PHP-featured

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.

gcp-storage-fileupload-create-bucket

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 “https://storage.googleapis.com/[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.

gcp-storage-fileupload-make-bucket-public

after saving and making it public

gcp-storage-fileupload-after-bucket-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

OR

gcp-storage-fileupload-create-service-account-step-1

step 2. granting roles to service account

OR

gcp-storage-fileupload-create-service-account-step-2

step 3. creating key

OR

gcp-storage-fileupload-create-service-account-step-3

gcp-storage-fileupload-create-service-account-step-4

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.

config.php

requests.php

index.php

Step 6: Upload File

gcp-storage-fileupload-step-1
Step 1
gcp-storage-fileupload-step-2
Step 2
gcp-storage-fileupload-step-3
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.

gcp-storage-fileupload-before-cors-configuration
Before CORS Configuration

gcp-storage-fileupload-after-cors-configuration
After CORS Configuration

NetBeans Project Download

References:-

https://cloud.google.com/storage/
https://cloud.google.com/storage/docs/key-terms#buckets
https://cloud.google.com/storage/docs/creating-buckets
https://cloud.google.com/cloud-console/
https://cloud.google.com/shell/
https://cloud.google.com/storage/docs/access-control/making-data-public
https://cloud.google.com/iam/docs/service-accounts
https://cloud.google.com/iam/docs/creating-managing-service-account-keys
https://getcomposer.org/
https://googleapis.github.io/google-cloud-php/
https://cloud.google.com/iam/docs/creating-managing-service-accounts
https://cloud.google.com/iam/docs/granting-roles-to-service-accounts
https://cloud.google.com/iam/docs/creating-managing-service-account-keys#iam-service-account-keys-create-gcloud

 

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.
https://www.zatackcoder.com

Comments

zatackcoder
Top