Using Cloud Storage

You can use Cloud Storage to store and serve files, such as movies or images or other static content.

This document describes how to use the Google Cloud Client Library in your app to store data in and retrieve data from Cloud Storage.

Before you begin

  • Follow the instructions in "Hello, World!" for PHP on App Engine to set up your environment and project, and to understand how PHP apps are structured in App Engine. Write down and save your project ID, because you will need it to run the sample application described in this document.

  • Make sure you create a Cloud Storage bucket for your application by invoking the following command:

    gsutil mb gs://[YOUR_BUCKET_NAME]
  • Make the bucket publicly readable so it can serve files:

    gsutil defacl set public-read gs://[YOUR_BUCKET_NAME]

Download the sample

To clone the repository:

git clone
cd php-docs-samples/appengine/flexible/storage

Edit project configuration and install dependencies

In app.yaml, set CLOUD_STORAGE_BUCKET; this value is the name of the Cloud Storage bucket you created previously.

  GOOGLE_STORAGE_BUCKET: "your-bucket-name"

In composer.json, notice that you must include the Cloud Client library, because this provides Cloud Storage functions.

    "require": {
        "slim/slim": "^4.0",
        "slim/psr7": "^1.3",
        "google/cloud-storage": "^1.0",
        "php-di/slim-bridge": "^3.1"

Application code

The sample application presents a web page prompting the user to supply a file to be stored in Cloud Storage. When the user selects a file and clicks submit, the upload handler loads the file content into a blob and writes it to Cloud Storage.

Notice that after the file is uploaded to Cloud Storage, the public URL to this file is returned, which you can use to serve the file directly from Cloud Storage. You should store this value in your app for future use.

use DI\Container;
use Google\Cloud\Storage\StorageClient;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Factory\AppFactory;

// Create App
AppFactory::setContainer($container = new Container());
$app = AppFactory::create();

// Display errors
$app->addErrorMiddleware(true, true, true);

$container = $app->getContainer();

$app->get('/', function (Request $request, Response $response) use ($container) {
    /** @var Google\Cloud\StorageClient */
    $storage = $container->get('storage');
    $bucketName = $container->get('bucket_name');
    $objectName = $container->get('object_name');
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->object($objectName);
    $content = $object->exists() ? $object->downloadAsString() : '';
    $escapedContent = htmlspecialchars($content);
    <h1>Storage Example</h1>
    <h3>Write [<a href="">docs</a>]:</h3>
    <form action="/write" method="post">
        Some file content:<br />
        <textarea name="content"></textarea><br />
        <input type="submit" />
    if ($content) {
            "<p><strong>Your content:</strong><p><p>$escapedContent</p>"
    return $response;

 * Write to a Storage bucket.
 * @see
$app->post('/write', function (Request $request, Response $response) use ($container) {
    /** @var Google\Cloud\StorageClient */
    $storage = $container->get('storage');
    $bucketName = $container->get('bucket_name');
    $objectName = $container->get('object_name');
    parse_str((string) $request->getBody(), $postData);
    $metadata = ['contentType' => 'text/plain'];
    $storage->bucket($bucketName)->upload($postData['content'] ?? '', [
        'name' => $objectName,
        'metadata' => $metadata,
    return $response
        ->withHeader('Location', '/');

$container->set('storage', function () use ($container) {
    $projectId = $container->get('project_id');
    $storage = new StorageClient([
        'projectId' => $projectId
    return $storage;

For more information

For complete information on Cloud Storage, see the Cloud Storage documentation.