In this article, you’ll learn how to deploy a PHP project to Google App Engine and how to upload an image to Google Cloud Bucket. Setting up a project in GAE is easy and can be done in a few minutes. The first step is to create a new project in App Engine.
STEP 1 - Creating a new project in App Engine
- Go to Google Cloud console (https://console.cloud.google.com) and log in with your Gmail id. Sign up for Google Cloud free tier services with which you will get a $300 credit with 12 months expiry.
- Now, on Google Cloud platform, from "products and services" tab, choose App Engine and click on dashboard.
- This will ask you to select/create a project. Create your project by giving a unique name. Also, please take note of your project-id which will be used while deploying the project.
- After creating your project, select your language and click Next.
Now, you can either complete a Google App Engine tutorial which will help you deploy a "hello world" application to your App Engine, or you can skip it and go to the next step of this article. Please note that on creating a new project in GAE , you will be assigned with default Cloud Storage Bucket with your project. To get your bucket name, go to App Engine > Settings.
Also, you can view your bucket at Products and Services > Storage > browser ..
STEP 2 - Deploying your project.
Now, you can deploy your project to the App Engine by either using shell commands in Google cloud shell or you can use Google cloud SDK to deploy your project. I will show here how to deploy using GAE SDK. You can download the SDK from this link.
- After installing Google Cloud SDK, open it and select "Create New Application".
- Enter your Project id (Created earlier) in Application name and choose runtime as PHP.
Click "Create".
- Now, your project folder will contain a app.yaml and main.php file. I have edited my main.php file so as to upload a image to the Cloud Bucket. The PHP code is as follows.
- <?php
- use google\appengine\api\cloud_storage\CloudStorageTools;
- $bucket = ‘ ';
- $root_path = 'gs://' . $bucket . '/';
- $_url = '';
- if(isset($_POST['submit']))
- {
- if(isset($_FILES['userfile']))
- {
- $name = $_FILES['userfile']['name'];
- $file_size =$_FILES['userfile']['size'];
- $file_tmp =$_FILES['userfile']['tmp_name'];
- $original = $root_path .$name;
- move_uploaded_file($file_tmp, $original);
- $_url=CloudStorageTools::getImageServingUrl($original);
- }
- }
-
- ?>
- <html>
-
- <body>
- <form action="#" method="post" enctype="multipart/form-data"> Send these files:
- <p/> <input name="userfile" type="file" />
- <p/> <input type="submit" name="submit" value="Send files" /> </form>
- </body>
-
- </html>
- <?php
-
- echo $_url;
-
- ?>
- Open GAE launcher SDK, select your project, and deploy.
- Go back to Google App Engine dashboard and open the link provided to you by App Engine. It can be found at the top-right corner of your dashboard.
- Upload an image.
Go to the Storage tab in the browser to see the uploaded images.
That’s it. I hope this article will help someone. The source code is attached with project.