PHP upload to Google Cloud Storage from App Engine

by

Google has a nice service where you can host your applications on their servers. There is a preview version of php available for App Engine. I wanted to see how hard it would be to write a php page allowing user to upload a file to Google cloud storage.

Build with PHP on the Cloud, Google Style

Let's make a simple App that allows a user to upload a file to Google cloud storage. We are going to do this on Windows 7 with php/python google App Engine sdk.

First setup a solid Development Environment on your windows pc

    • Install Python 2.7.x (any version of 2.7)
       
      Download Python 2.7.x

      (https://www.python.org/downloads/windows/) The local developer App Engine server needs python to communicate with Google Cloud services.

       
       
    • AppEngine PHP SDK "original sdk"
         
    • A Code Editor
       
        A code editor with file system with sidebar view of file system to show your app engine project folder. Try Editra for windows but there are others for example: Eclipse, sublimetext, notepad++, dreamweaver  
       
 

Development with PHP and App Engine

  • Use App Launcher
     
      Use App Launcher installed with App Engine PHP SDK (File -> Create New Application)  
     
  • project-id for application name
     
      Use project-id for application name. Project id will be listed in developer's console  
     
  • Edit app.yaml
     
      Open your favorite code editor and edit app.yaml from your local application folder. App Engine will create default favicon.ico, app.yaml, and index.php files. Edit app.yaml file. After editing app.yaml, we will be creating a upload.php file, php.ini file. First, let's modify app.yaml. app.yaml has your application settings. In this file you give your Application a version number, Select php as runtime. Most importantly you set handlers to route links on your website to script files and static files.    
    
                application: props-trt-526
                version: alpha003
                runtime: php
                api_version: 1
                threadsafe: yes

                handlers:
                - url: /favicon\.ico
                  static_files: favicon.ico
                  upload: favicon\.ico
                - url: /inc
                  static_dir: inc

                - url: /
                  script: main.php

                - url: /upload
                  script: upload.php
    In this app.yaml any link with http://your-project-id.appspot.com/inc in the application will point to inc folder. We will use inc folder for hosting css, and image static files. We could have created a url for /css , /images to organize things. You can name this anything you like and you can even have more then one static folder. A static folder can host other static folders inside without having to define each one in app.yaml  
     
  • Create a storage bucket
     
      Create a storage bucket inside your project using point and click on Google cloud console. In the cloud console click "Cloud Storage" and press the button to create a storage bucket.  
     
  • Create upload.php
     
      First few lines of upload.php is the main code that sets things up. First few lines call Google cloud storage and creates a URL for your php script to upload a file from $_POST request. Topcoat css is a user interface library used to make the page look decent to use. Upload.php begins with four lines of code provided in excellent documentation by Google.  
    
       require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
       use google\appengine\api\cloud_storage\CloudStorageTools;

       $options = [ 'gs_bucket_name' => 'please-enter-bucket-name' ];
       $upload_url = CloudStorageTools::createUploadUrl('/upload', $options);
    Here is the entire upload.php code view  
    
    <!--?php

       require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
       use google\appengine\api\cloud_storage\CloudStorageTools;

       $options = [ 'gs_bucket_name' =--> 'please-enter-bucket-name' ];
       $upload_url = CloudStorageTools::createUploadUrl('/upload', $options);

    ?&gt;




    <!--?php

    if(isset($_POST['do-upload']) AND $_POST['do-upload'] === "yes"){

       $yesupload = $_POST['do-upload'];
       preg_match("/yes/", "".$yesupload."");

       $filename = $_FILES['testupload']['name'];
       
       $gs_name = $_FILES['testupload']['tmp_name'];
       move_uploaded_file($gs_name, 'gs://please-enter-bucket-name/'.$filename.'');

    ?-->
    Hey file is uploaded, Yes! ".$yesupload." !  "; echo "

    This is the file name: ".$filename."

    "; } echo "
    
    ";
    ?&gt;
    Files to upload:
    
    &nbsp;
    
    When page loads the script on top of the file has retrieved a upload url from google cloud storage and it is set now as a form action for post request.
    When a user presses upload button the same page is loaded again. This time the if statement is in action since form was submitted and hidden value of "do-upload" is yes. The if statement grabs file name and passes the file data to google cloud storage.    
  • Create and Edit php.ini file for larger files
     
      For allowing this script to upload files you need a long enough session time(session.gc_maxlifetime 10000 sec) to let http post request send data(images, audios, videos, csv, xml and so on) to google cloud storage and you also need to set a higher upload limit(max_file_uploads 100 mb) to allow AppEngine WebServer upload to Google Cloud Storage here is how php.ini looks:  
    
                ; This is a simple php.ini file on App Engine
                ; It enables output buffering for all requests by overriding the
                ; default setting of the PHP interpreter.
                output_buffering = "On"
                max_file_uploads = 0
                upload_max_filesize = 100M
                session.gc_maxlifetime = 10000

     
     

Let's Test out the upload script

Use App Engine launcher to deploy the app using your account id. App will be usable on http://your-project-id.appspot.com/upload

Upload to Google cloud
  This is how the page looks because I am using topcoat and added a button class from topcoat css. It's your basic upload form but behind the scene there is cloud storage and you didn't have to setup any vps server, configure it, or use ftp to upload your files. Simply press deploy button on your php App Engine sdk and script on your computer uploads your project and makes it live. You can have multiple versions and if you make a mistake visit developer console website and switch back to previous version.

Download Complete Code from Github Repo

I've created a repo on Github with App Engine PHP upload app ready to go. You can download from here. Use this Git Repo If you are trying to avoid the 32MB limitation Let's connect this to CloudSQL send
Z Data Tech https://www.zdatatech.com/logo/zdatatech-logo.png Last modified: January 30, 2023
Suggested
Advertisement