Skip to content

Commit 800f946

Browse files
authored
Adds Cloud Storage sample for AppEngine Flex (GoogleCloudPlatform#279)
1 parent 27f3836 commit 800f946

File tree

10 files changed

+3024
-0
lines changed

10 files changed

+3024
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Cloud Storage & Google App Engine
2+
3+
This sample application demonstrates how to use [Cloud Storage with Google App Engine](https://cloud.google.com/appengine/docs/flexible/php/using-cloud-storage).
4+
5+
## Setup
6+
7+
Before running this sample:
8+
9+
### Register your application
10+
11+
- Go to
12+
[Google Developers Console](https://console.developers.google.com/project)
13+
and create a new project. This will automatically enable an App
14+
Engine application with the same ID as the project.
15+
16+
- Go to
17+
[Google Cloud Storage](https://console.cloud.google.com/storage/browser)
18+
and create a new bucket. Optionally, use the default bucket for your project
19+
(`YOUR_BUCKET.appspot.com`).
20+
21+
### Prerequisites
22+
23+
- Install [`composer`](https://getcomposer.org)
24+
- Install dependencies by running:
25+
26+
```sh
27+
composer install
28+
```
29+
30+
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
31+
- Initialize the SDK by running `gcloud init`
32+
33+
## Run Locally
34+
35+
Set the environment variables `GOOGLE_BUCKET_NAME` and `GCLOUD_PROJECT` to the name of your storage bucket and project ID respectively.
36+
37+
```
38+
export GOOGLE_BUCKET_NAME=your-bucket-name
39+
export GCLOUD_PROJECT=your-project-id
40+
```
41+
42+
Run the sample with the PHP built-in web server:
43+
44+
```
45+
php -S localhost:8080
46+
```
47+
48+
> Note: Your PHP executable path may be different than the one above.
49+
50+
Now browse to `http://localhost:8080` to view the sample.
51+
52+
## Deploy to App Engine
53+
54+
**Prerequisites**
55+
56+
- Set `your-bucket-name` in `app.yaml` to the name of your Cloud Storage Bucket.
57+
58+
**Deploy with gcloud**
59+
60+
```
61+
gcloud config set project YOUR_PROJECT_ID
62+
gcloud app deploy
63+
gcloud app browse
64+
```
65+
66+
The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/`
67+
in your browser.

appengine/flexible/storage/app.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
# [START import_client]
19+
use Google\Cloud\Storage\StorageClient;
20+
# [END import_client]
21+
use Silex\Application;
22+
use Symfony\Component\HttpFoundation\Request;
23+
24+
// create the Silex application
25+
$app = new Application();
26+
27+
$app->get('/', function () use ($app) {
28+
/** @var Google\Cloud\StorageClient */
29+
$storage = $app['storage'];
30+
$bucketName = $app['bucket_name'];
31+
$objectName = $app['object_name'];
32+
$bucket = $storage->bucket($bucketName);
33+
$object = $bucket->object($objectName);
34+
$content = $object->exists() ? $object->downloadAsString() : '';
35+
$escapedContent = htmlspecialchars($content);
36+
$form = <<<EOF
37+
<h1>Storage Example</h1>
38+
<h3>Write [<a href="https://cloud.google.com/appengine/docs/flexible/php/using-cloud-storage">docs</a>]:</h3>
39+
<form action="/write" method="post">
40+
Some file content:<br />
41+
<textarea name="content"></textarea><br />
42+
<input type="submit" />
43+
</form>
44+
EOF;
45+
if ($content) {
46+
$form .= "<p><strong>Your content:</strong><p><p>$escapedContent</p>";
47+
}
48+
return $form;
49+
});
50+
51+
/**
52+
* Write to a Storage bucket.
53+
* @see https://cloud.google.com/appengine/docs/flexible/php/using-cloud-storage
54+
*/
55+
$app->post('/write', function (Request $request) use ($app) {
56+
/** @var Google\Cloud\StorageClient */
57+
$storage = $app['storage'];
58+
$bucketName = $app['bucket_name'];
59+
$objectName = $app['object_name'];
60+
$content = $request->get('content');
61+
# [START write]
62+
$metadata = ['contentType' => 'text/plain'];
63+
$storage->bucket($bucketName)->upload($content, [
64+
'name' => $objectName,
65+
'metadata' => $metadata,
66+
]);
67+
# [END write]
68+
return $app->redirect('/');
69+
});
70+
71+
$app['storage'] = function () use ($app) {
72+
$projectId = $app['project_id'];
73+
# [START create_client]
74+
$storage = new StorageClient([
75+
'projectId' => $projectId
76+
]);
77+
# [END create_client]
78+
return $storage;
79+
};
80+
81+
return $app;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
runtime: php
2+
vm: true
3+
4+
# [START env_variables]
5+
env_variables:
6+
GOOGLE_BUCKET_NAME: "your-bucket-name"
7+
# [END env_variables]
8+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"require": {
3+
"silex/silex": "^1.3",
4+
"google/cloud": "^0.20"
5+
},
6+
"require-dev": {
7+
"symfony/browser-kit": "^3.0",
8+
"google/cloud-tools":"^0.6"
9+
}
10+
}

0 commit comments

Comments
 (0)