Using Cloud Storage with PHP

This part of the PHP Bookshelf tutorial shows how to store images in Google Cloud Storage.

This page is part of a multi-page tutorial. To start from the beginning and read the setup instructions, go to PHP Bookshelf app.

Creating a Cloud Storage bucket

The following instructions show how to create a Cloud Storage bucket. Buckets are the basic containers that hold your data in Cloud Storage.

  1. In a terminal window, enter the following command:

    gsutil mb gs://[YOUR-BUCKET-NAME]


    • [YOUR-BUCKET-NAME] represents the name of your Cloud Storage bucket.
  2. To view uploaded images in the bookshelf app, set the bucket's default access control list (ACL) to public-read.

    gsutil defacl set public-read gs://[YOUR-BUCKET-NAME]

    Configuring settings

    Copy your settings.yml file from the Structured Data part of this tutorial to the getting-started-php/3-cloud-storage directory.

    Installing dependencies

    In the 3-cloud-storage directory, enter this command.

    composer install

    Running the app on your local machine

    1. Start a local web server:

      php -S localhost:8000 -t web
    2. In your web browser, enter this address.


    Now you can browse the app's web pages, add books with cover images, edit books, and delete books.

    Deploying the app to the App Engine flexible environment

    1. Deploy the sample app:

      gcloud app deploy
    2. In your web browser, enter this address. Replace [YOUR_PROJECT_ID] with your project ID:


      If you update your app, you can deploy the updated version by entering the same command you used to deploy the app the first time. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app remain, as do their associated VM instances. Be aware that all of these app versions and VM instances are billable resources.

      You can reduce costs by deleting the non-default versions of your app.

      To delete an app version:

      1. In the GCP Console, go to the App Engine Versions page.

        Go to the Versions page

      2. Click the checkbox next to the non-default app version you want to delete.
      3. Click the Delete button at the top of the page to delete the app version.

      For complete information about cleaning up billable resources, see the Cleaning up section in the final step of this tutorial.

      Application structure

      Binary data sample structure

      The application uses Cloud Storage to store binary data (pictures in this case), while continuing to use a structured database for the book information: either Cloud Datastore, Cloud SQL, or MongoDB.

      Understanding the code

      The template for listing the books in your database receives a books array variable. For each book in the array, the template displays the cover image specified by the book.imageUrl. If the book doesn't have an imageUrl member, the template displays a default image:

      <div class="media-left">
        {% if book.image_url %}
          <img src="{{book.image_url}}">
        {% else %}
          <img src="">
        {% endif %}

      The template for displaying an individual book does the same thing:

      <div class="media-left">
        <img class="book-image"
             src="{{ book.image_url ?: '' }}">

      The form for entering or editing a book includes a field where the user can enter a URL for the cover image:

      <div class="form-group">
        <label for="image">Cover Image</label>
        <input type="file" name="image" id="image" class="form-control"/>
      <div class="form-group hidden">
        <label for="image_url">Cover Image URL</label>
        <input type="text" name="image_url" id="image_url" value="{{book.image_url}}" class="form-control"/>

      When the user deletes a book, the handler for the POST '/books/{id}/delete' route deletes the book's cover image from Cloud Storage. The $storage variable is an instance of the CloudStorage class, which is defined in src/FileSystem/CloudStorage.php.

      if (!empty($book['image_url'])) {
          /** @var CloudStorage $storage */
          $storage = $app[''];
Was this page helpful? Let us know how we did:

Send feedback about...