You are here
Home > Php > Connect Google App Engine with Google Cloud Storage

Connect Google App Engine with Google Cloud Storage

app-engine-with-cloud-storage-featured
Hello Friends, I hope you are doing well. Today we are going to see how to connect Google App Engine with Google Cloud Storage. App Engine is a fully managed, serverless platform for developing and hosting web applications at scale while Cloud Storage is a RESTful file storage service for storing and accessing data on Google Cloud Platform. Google Cloud Storage service can be used to serve website content, store data for archival and disaster recovery or distributing large files to users via direct download. Here we are going to develop a one page app which will give us options to upload file, list files as well as delete files. So, without wasting any time let’s do it by following below steps:-

Step 1: Login to Google Cloud Console and Create a Project

app-engine-with-cloudstorage-create-project

 

Step 2: Go to App Engine and Create Application using below command in Cloud Shell or you can also follow steps by clicking on create application see screenshots below for your reference

OR

app-engine-with-cloudstorage-create-application-step-1 app-engine-with-cloudstorage-create-application-step-3 app-engine-with-cloudstorage-create-application-step-4
Step 1 Step 2 Step 3 Step 4

Step 3: After receiving success open Cloud Shell if not already opened and click on pencil icon to Launch Editor

app-engine-with-cloudstorage-open-editor

Step 4: Download Google Cloud Storage Library using below command

Composer comes pre-installed on Cloud Shell so you need to install it, but still if you need guidance to install composer you check here https://getcomposer.org/.
After executing above command you’ll see vendor directory which contains cloud storage library and two more files called composer.json and composer.lock

Step 5: Now It’s Time to Code

app.yaml

We configure our App Engine app’s settings in the app.yaml file. The app.yaml file also contains information about your app’s code, PHP runtime, and entrypoint.

Here we are defining an environment variable named as BUCKET_NAME which we will use in our code, this bucket name you can get from Navigation Menu => Storage Option in GCP Console. Actually, when we create App Engine project a bucket is automatically created by App Engine for the use in your app with named as “yourprojectid.appspot.com”.

config.php

All necessary functions for uploading, listing and deleting files are defined here in this file

index.php

User requests routing code which responds according to path i.e opening home page sending ajax request to requests.php showing 404 on invalid path

home.php

Code to list objects of given bucket, uploading and deleting objects (files)

requests.php

This file is called through ajax to upload file on google cloud storage and here you can see $cloudPath variable which is used to store file path and if we need to store it in database we can do so and later we can fetch it from our database instead of cloud storage api. We can also use Cloud SQL in our application to make our application fully cloud based if you are not aware how to use Cloud SQL with App Engine then checkout this post Connect Google App Engine with Google Cloud SQL.

Step 6: Go to Editor which we had launched in step 3 and upload all the files created in step 5.

app-engine-with-cloud-storage-upload-files app-engine-with-cloud-storage-files-uploaded

Step 7: Deploy App using below command

app-engine-with-cloud-storage-deploy-app

Step 8: Access App

app-engine-with-cloud-storage-accessing-app

 

Note: –

We don’t have to create service account and download private key just as we had done in Upload File to Google Cloud Storage using PHP article as this code is running on App Engine and App Engine app by default connected to a service account which have access to Cloud Storage.

Video

NetBeans Project Download

References:-

https://cloud.google.com/appengine/docs/standard/php7/config/appref
https://cloud.google.com/appengine/docs/standard/php7/using-cloud-storage
https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-code-sample
https://cloud.google.com/storage/docs/access-control/making-data-public#storage-make-object-public-php
https://cloud.google.com/storage/docs/listing-objects#storage-list-objects-php
https://cloud.google.com/storage/docs/deleting-objects#storage-delete-object-php

 

Thank you Friends
Please don’t forget share if you it useful

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