Home » , » Guide – Steps to Connect PHP Application to Google Cloud Storage

Guide – Steps to Connect PHP Application to Google Cloud Storage

jaki watson | 04:49 | 0 comments
PHP Cloud Base Solution
There is no doubt Google has the best service to host an application on its servers. Now, users can have a preview version of PHP available for App Engine. in this article, you will get to know what advantages of Google’s Platform as a Service (PaaS) to develop highly available and secure PHP web application.

Please note that these instructions are especially for Mac OSX, but it may work for LINUX as well. Some modifications may be required for Windows.

Google Cloud Platform

First of all, you have to require a Google account. And after that, download the cloud SDK.

curl https://sdk.cloud.google.com/ | bash

One may have to start a new terminal and after that, have to authenticate yourself by typing : gcloud auth login

Then, you will be asked to permit some access. ‘Allow’ it. At last, you have to install the App Engine PHP package by typing:

gcloud components update gae-python

You have to avoid alarmed by the "python" suffix. This is the proper package that includes PHP and Python both. It accomplishes the initial steps for getting ready for general Google App Engine development with PHP.

Google App Engine

You must want to know that what’s Google App Engine? And why you should use as the deployment target for your PHP application? There are many good reasons behind it but some of them are:

  • Auto scaling is available if demand fluctuates
  • Solid and mature deployment platform for web applications
  • App Engine works with many IDEs that make easy development
  • The cost of small applications is free
  • Infrastructure is managed by 24*7 by Google as per industry best practices

Google Cloud Storage

In order to use Google Cloud Storage, one can have to require a free trial that needs offering Google with billing information as verification. There are many data storage options provided by Google and Google Cloud Storage is one of them. Below given are some reasons that tell why it is must to keep your data in Google cloud storage:
  • The price up to 5GB is right (free)
  • Google management through console
  • Good access control
  • Google stores your data for you
  • REST APIs

Create a PHP Application on GAE

Let’s move forward to create a project directory (e.g. ~/random_meme) and place these files in it:

-- -- start app.yaml -- -- -
application: random-meme
version: 1
runtime: php55
api_version: 1
handlers:
- url: /.*
script: main.php
-- -- end app.yaml -- -- -
-- - start main.php -- -- -
<html>
<head>
<title>Random Meme</title>
</head>
<body>
<?php
$input = array(
"http://d2ws0xxnnorfdo.cloudfront.net/meme/300.jpg",
"http://d2ws0xxnnorfdo.cloudfront.net/meme/301.jpg",
"http://d2ws0xxnnorfdo.cloudfront.net/meme/302.jpg",
"http://d2ws0xxnnorfdo.cloudfront.net/meme/303.jpg",
"http://d2ws0xxnnorfdo.cloudfront.net/meme/305.jpg",
"http://d2ws0xxnnorfdo.cloudfront.net/meme/307.jpg");
$rand_key = array_rand($input, 1);
$image = $input[$rand_key];
echo '<img src="' . $image . '">'; ?>
</body>
</html>
-- - end main.php -- -- -


That ‘s it. Now, you just have to create a random meme web application. For that, you have to upload it to the Google App Engine.

cd <parent dir of the randome-meme directory>
appcfg.py -A random-meme update randome-meme/

After that, you have to browse to random-meme.appspot.com and check out random meme. If you want to get new one then refresh once.

Upload the Memes to Google Cloud Storage by creating a bucket named as random-meme-bucket

Now, one can easily use the Google console for uploading the list of memes from the previous section as a simple text file. Here, you will find each line with one meme:

-- -- - start memes.txt -- -- --
http://d2ws0xxnnorfdo.cloudfront.net/meme/300.jpg
http://d2ws0xxnnorfdo.cloudfront.net/meme/301.jpg
http://d2ws0xxnnorfdo.cloudfront.net/meme/302.jpg
http://d2ws0xxnnorfdo.cloudfront.net/meme/303.jpg
http://d2ws0xxnnorfdo.cloudfront.net/meme/305.jpg
http://d2ws0xxnnorfdo.cloudfront.net/meme/307.jpg
-- -- - end memes.txt -- -- --

Google Developer's console can also allow doing such thing.



Now, Connect Your Application with Google Cloud Storage

You will surely find your memes in the cloud that we can fetch from Google cloud storage rather than of hard-coding them as an array inside our application.

Here is the code:

<html>
<head>
<title>Random Meme</title>
</head>
<body>
<?php
$memes = file_get_contents('gs://random-meme-bucket/memes.txt');
$input = preg_split("/[\s,]+/", $memes);
$rand_key = array_rand($input, 1);
$image = $input[$rand_key];
echo '<img src="' . $image . '">'; ?>
</body>
</html>

Google Cloud Storage object URLs supported by the file_get_contents()PHP function on Google App Engine to fetch the contents of memes.txt as a string. After that, the next line parses it into an array of strings. Here, each one is a URL to a meme image and rest is the same. You have to choose a random image and output it as an img tag into the page.

Note: Remember to update your application.

appcfg.py -A random-meme update randome-meme/

If you are looking for an experienced PHP developers, who can do everything for you, then contact Perception System now.
Share this article :

0 comments:

Post a Comment

 
Design by: