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 a flexible environment 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 publically readable so it can serve files:

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

Download the sample

To clone the repository:

git clone https://github.com/GoogleCloudPlatform/php-docs-samples
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.

env_variables:
  GOOGLE_BUCKET_NAME: "your-bucket-name"

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

{
    "require": {
        "silex/silex": "^1.3",
        "google/cloud-storage": "^1.0"
    },
    "require-dev": {
        "symfony/browser-kit": "^3.0",
        "google/cloud-tools":"^0.6"
    }
}

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 Google\Cloud\Storage\StorageClient;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;

// create the Silex application
$app = new Application();

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

/**
 * Write to a Storage bucket.
 * @see https://cloud.google.com/appengine/docs/flexible/php/using-cloud-storage
 */
$app->post('/write', function (Request $request) use ($app) {
    /** @var Google\Cloud\StorageClient */
    $storage = $app['storage'];
    $bucketName = $app['bucket_name'];
    $objectName = $app['object_name'];
    $content = $request->get('content');
    $metadata = ['contentType' => 'text/plain'];
    $storage->bucket($bucketName)->upload($content, [
        'name' => $objectName,
        'metadata' => $metadata,
    ]);
    return $app->redirect('/');
});

$app['storage'] = function () use ($app) {
    $projectId = $app['project_id'];
    $storage = new StorageClient([
        'projectId' => $projectId
    ]);
    return $storage;
};

For more information

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

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

App Engine flexible environment for PHP docs