|
| 1 | +
|
| 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 auth_cloud_explicit_app_engine] |
| 19 | +use Google\Auth\Credentials\AppIdentityCredentials; |
| 20 | +use Google\Cloud\Storage\StorageClient; |
| 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 | + # Explicitly use service account credentials by using App Engine credentials. |
| 29 | + # Learn more about scopes at https://cloud.google.com/storage/docs/authentication#oauth-scopes |
| 30 | + $projectId = $app['project_id']; |
| 31 | + $scope = 'https://www.googleapis.com/auth/devstorage.read_only'; |
| 32 | + $gae = new AppIdentityCredentials($scope); |
| 33 | + $config = [ |
| 34 | + 'projectId' => $projectId, |
| 35 | + 'credentialsFetcher' => $gae, |
| 36 | + ]; |
| 37 | + $storage = new StorageClient($config); |
| 38 | + |
| 39 | + # Make an authenticated API request (listing storage buckets) |
| 40 | + $buckets = $storage->buckets(); |
| 41 | + # [END auth_cloud_explicit_compute_engine] |
| 42 | + |
| 43 | + $content = ''; |
| 44 | + foreach ($buckets as $bucket) { |
| 45 | + $content .= $bucket->name(); |
| 46 | + $content .= ', '; |
| 47 | + } |
| 48 | + $content = substr($content, 0, -2); |
| 49 | + $escapedContent = htmlspecialchars($content); |
| 50 | + $html = "Storage Buckets"; |
| 51 | + if ($content) { |
| 52 | + $html .= "Your Cloud Storage buckets: $escapedContent"; |
| 53 | + } |
| 54 | + return $html; |
| 55 | +}); |
| 56 | + |
| 57 | +return $app; |
| 58 | + |
0 commit comments