PHP API Client Library and Google Cloud Storage

Google_Cloud_Platform

There is very few resources and/or examples of the Google PHP API Client Library working with Google Cloud Storage, so here are some basics things I’ve learned.

My project is currently hosted on a Web Server and allow us to store and retrieve public readable pictures on Google Cloud Storage.

Requirements & Installation

  • PHP version 5.4.0 or greater.
  • JSON PHP extension.
  • Support for a writeable file system or Memcache.
  • cURL for the cURL IO method, or allow_url_fopen enabled for the Streams IO method.

You can install the library by adding it as a dependency to your composer.json.

Or take the latest source code from GitHub.
Upon installation, include the autoloader.php in your scripts.

or something like this for GitHub

Google Cloud Storage Credentials

You can find resources for this step in Google Developers Console Website but here is what I’ve done.

  • Go to the Google Developers Console
  • From the projects list, select a project or create a new one.
  • Open the menu (On the top left) and select API Manager. Then click on Credentials.
  • Click on New Credentials, then choose Service Account Key.
  • On the next screen, select a New Service Account, fill the blanks and choose your Key Type (P12 here).
  • Save the P12 file in your project.
  • Grab the Account Email for your project on the Permissions Page (Top left menu), something like XXX-5421@{Project Name}.iam.gserviceaccount.com

Setting the Authentication in PHP

You will notice I use a prefix, this is because each bucket name in Google Cloud Storage is unique and shared between everyone.

With this code, you have your full control access to Google Cloud Storage.

Basic Commands

Google_Service_Storage object is simple to use, you access your Objects & Buckets via two public properties.

Access to a Bucket

This will throw an Exception if your Bucket is missing.
Remember that you don’t have to load an Object’s Bucket to access it (In fact it will cost you money to do so).

Create a Bucket

To create a Bucket, make a Google_Service_Storage_Bucket object, set your Bucket name & location and you’re done.

Find your Google Cloud Storage Bucket locations.

Insert an Object

To upload an Object, make a Google_Service_Storage_StorageObject, and call the Google_Service_Storage->objects->insert() method.

You can see here how we handled the fact that we uploaded a picture by setting the mimeType and data.
Because we want to download our picture and crop it as fast as possible we used a Predefined ACL enabling the public access to the file, without a slow and costly authentication to Google Cloud Storage.

If you find Google Cloud Storage read access is really slow and you have some kind of public data (Like picture) enable this ACL, you will notice a huge difference in access times.

Delete an Object

Simple command

Access an Object

With our public read policy this is very simple too, use it without authentication.

In fact the public access URL for all our pictures is something like https://storage.googleapis.com/project_picture_bucket/my_picture.png

Documentation

Get started with Google Api Client Library

2 Responses

  1. Jeremy says:

    Hello,
    What’s the meaning of ‘prefix’ => ‘project_picture_’ in the configuration array ? are we free to put anything or does it have to be something specific, if yes then where do we find its value ?

  2. elijaa says:

    Hello,

    You are free to put anything.
    We have a lot of bucket so we wanted to be able to create them automatically without taking risks of it’s name already taken, hence the long prefix.

Leave a Reply to elijaa Cancel reply

Your email address will not be published. Required fields are marked *