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.
1 2 3 |
"require": { "google/apiclient": "1.0.*@beta" } |
Or take the latest source code from GitHub.
Upon installation, include the autoloader.php in your scripts.
1 |
require 'vendor/autoload.php'; |
or something like this for GitHub
1 |
require 'lib/Google/autoload.php'; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$configuration => array( 'login' => 'service-account@project-picture-119480.iam.gserviceaccount.com', 'key' => file_get_contents({Your P12 Key Location}), 'scope' => 'https://www.googleapis.com/auth/devstorage.full_control', 'project' => 'project-picture-119480', 'storage' => array( 'url' => 'https://storage.googleapis.com/', 'prefix' => 'project_picture_')), # Making Credential for API $credentials = new Google_Auth_AssertionCredentials($configuration['login'], $configuration['scope'], $configuration['key']); # Creating Client & Applying Credentials $client = new Google_Client(); $client->setAssertionCredentials($credentials); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion(); } # Starting Webmaster Tools Service $storage = new Google_Service_Storage($client); |
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.
1 2 |
$storage->objects; # To handle your Objects $storage->buckets; # To handle your Buckets |
Access to a Bucket
1 |
$storage->buckets->get($configuration['storage']['prefix'] . $bucketName, array('projection' => 'full')); |
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.
1 2 3 4 |
$bucket = new Google_Service_Storage_Bucket(); $bucket->setName($configuration['storage']['prefix'] . $bucketName); $bucket->setLocation('EUROPE-WEST1'); $storage->buckets->insert($configuration['project'], $bucket); |
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.
1 2 3 4 5 6 7 8 9 10 11 |
$picture = new Picture(); # HomeMade Object $object = new Google_Service_Storage_StorageObject(); $object->setName($path); # Object Name or "Path" self::$_storage->objects->insert($configuration['storage']['prefix'] . $bucket, $object, array('uploadType' => 'media', 'mimeType' => 'image/' . $picture->contentType(), 'data' => $picture->blob(), 'predefinedAcl' => 'publicRead')); |
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
1 |
$storage->objects->delete($configuration['storage']['prefix'] . $bucket, $path); # Object Name or "Path" |
Access an Object
With our public read policy this is very simple too, use it without authentication.
1 |
file_get_contents($configuration['storage']['url'] . $configuration['storage']['prefix'] . $bucket . '/' . $path); |
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