Connecting Your PHP Application to Google Cloud Storage
We all know that PHP is widely used in developing websites as well as applications. Here, in this blog post we will see how Google’s Platform as a Service can be used in order to develop a scalable and secure PHP Web Application. All the instructions given here are for Mac Operating System.
Connecting PHP Application with Google Cloud:
First of all, you must have a Google account and then you need to download cloud SDK. You can download this from here: “curl https://sdk.cloud.google.com/ | bash”
Now, you may need to create a new terminal and then get yourself authenticated by typing:
|gcloud auth login
Here, you will have to allow some access and then install the App Engine PHP Package by typing:
|gcloud components update gae-python
This is the right package which includes both PHP and Python. These are the basic steps to be ready for General Google App Engine development with PHP.
Google App Engine:
Google App Engine can be used as a deployment target for your PHP application. There are several reasons for using this app engine:
- Solid and mature deployment platform for web applications
- Auto scaling is available when demand is fluctuating
- Infrastructure is managed 24/7 by Google as per industry best practices.
- App Engine works with several different IDEs. Hence, it is easy to develop.
- Small Applications are available for free.
Google Cloud Storage:
If you wish to use Google cloud storage then first of all start a free trial where in you need to provide proper billing information to Google as a means of verification. Google offers several data storage options.
Google Cloud storage is one of the simplest and most popular ways of storing the data. Here are some of the reasons for using Google cloud storage:
- Google stores the data for you.
- Google manages data via Console
- Good Access control
- REST APIs
- It is available for free up to 5 GB.
Developing a PHP Application on GAE:
Here, we will see how a PHP application can be developed on GAE. We will develop a project directory and add a file in it:
This is the complete code for developing random web application and then this is to be uploaded on Google App Engine.
cd <parent dir of the randome-meme directory>
appcfg.py -A random-meme update randome-meme/
Now, you can easily browse your random meme.appspot.com and view your random meme.
Uploading Memes to Google Cloud Storage:
Google console is used to upload the list of memes from the previous section in a simple text file where each line will have one meme.
This can be done from Google’s developer console.
Now, is the time to connect PHP Application to Google Cloud Storage:
We have created four memes in the cloud and we can fetch them from Google cloud storage instead of using hard codes in an array in our application.
The code for the same is as follows:
Here, the function “file_get_contents”on Google App Engine supports Google Cloud storage object URLs. The contents of memes.txt are to be fetched as a string. The next line then gets involved into a complete array of strings each one is a URL to a meme image.
Everything else is same. Just select a random image and output it as an img tag into the page. Remember to update your app.
Hope this blog post will be useful to you whenever you want to connect your any of the PHP applications with Google Cloud storage. Let us know if you have found any better way to do so.
For more of such information about PHP, stay connected with Softqube Technologies, PHP Development Service Provider in India.