Serving Images

The App Engine API for Cloud Storage Tools provides convenience methods for serving image files:

  • CloudStorageTools.getImageServingUrl()
  • CloudStorageTools.deleteImageServingUrl()

One advantage of using this method to serve images over simply making the files public is the ability to resize and crop dynamically, without needing to store the images in different sizes.

CloudStorageTools::getImageServingUrl returns a serving URL for an image. If the image will be displayed within an HTTPS page, set secure_url to True to avoid mixed-content warnings.

Notice that this URL is publicly readable by everyone, but it is not "guessable".

To stop serving the URL, call CloudStorageTools::deleteImageServingUrl.

To use this feature, import the CloudStorageTools class:

use google\appengine\api\cloud_storage\CloudStorageTools;

Now resize and crop the image image.jpg:

$options = ['size' => 400, 'crop' => true];
$image_file = "gs://${my_bucket}/image.jpg";
$image_url = CloudStorageTools::getImageServingUrl($image_file, $options);