PHP API Client Library and Google Cloud Storage

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}

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


Get started with Google Api Client Library

Leave a Reply