|
| 1 | +
|
| 2 | +/** |
| 3 | + * Copyright 2012 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 | +/** |
| 19 | + * Follow the instructions on https://github.com/google/google-api-php-client |
| 20 | + * to download, extract and include the Google APIs client library for PHP into |
| 21 | + * your project. |
| 22 | + */ |
| 23 | +require_once 'google-api-php-client/autoload.php'; |
| 24 | + |
| 25 | +session_start(); |
| 26 | + |
| 27 | +/** |
| 28 | + * Visit https://console.developers.google.com to generate your |
| 29 | + * oauth2_client_id, oauth2_client_secret, and to register your |
| 30 | + * oauth2_redirect_uri. |
| 31 | + */ |
| 32 | +$client = new Google_Client(); |
| 33 | +$client->setApplicationName("Google Cloud Storage PHP Starter Application"); |
| 34 | +$client->setClientId('YOUR_CLIENT_ID'); |
| 35 | +$client->setClientSecret('YOUR_CLIENT_SECRET'); |
| 36 | +$client->setRedirectUri('YOUR_REDIRECT_URI'); |
| 37 | +$client->setDeveloperKey('YOUR_API_KEY'); |
| 38 | +$client->setScopes('https://www.googleapis.com/auth/devstorage.full_control'); |
| 39 | +$storageService = new Google_Service_Storage($client); |
| 40 | + |
| 41 | +/** |
| 42 | + * Constants for sample request parameters. |
| 43 | + */ |
| 44 | +define('API_VERSION', 'v1'); |
| 45 | +define('DEFAULT_PROJECT', 'YOUR_DEFAULT_PROJECT_ID'); |
| 46 | +define('DEFAULT_BUCKET', 'YOUR_DEFAULT_BUCKET_NAME'); |
| 47 | +define('DEFAULT_OBJECT', 'YOUR_DEFAULT_OBJECT_NAME'); |
| 48 | + |
| 49 | +/** |
| 50 | + * Generates the markup for a specific Google Cloud Storage API request. |
| 51 | + * @param string $apiRequestName The name of the API request to process. |
| 52 | + * @param string $apiResponse The API response to process. |
| 53 | + * @return string Markup for the specific Google Cloud Storage API request. |
| 54 | + */ |
| 55 | +function generateMarkup($apiRequestName, $apiResponse) { |
| 56 | + $apiRequestMarkup = ''; |
| 57 | + $apiRequestMarkup .= "" . $apiRequestName . ""; |
| 58 | + |
| 59 | + if ($apiResponse['items'] == '' ) { |
| 60 | + |
| 61 | + $apiRequestMarkup .= print_r(json_decode(json_encode($apiResponse), true), |
| 62 | + true); |
| 63 | + $apiRequestMarkup .= ""; |
| 64 | + } else { |
| 65 | + foreach($apiResponse['items'] as $response) { |
| 66 | + |
| 67 | + $apiRequestMarkup .= print_r(json_decode(json_encode($response), true), |
| 68 | + true); |
| 69 | + $apiRequestMarkup .= ""; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return $apiRequestMarkup; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Clear access token whenever a logout is requested. |
| 78 | + */ |
| 79 | +if (isset($_REQUEST['logout'])) { |
| 80 | + unset($_SESSION['access_token']); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Authenticate and set client access token. |
| 85 | + */ |
| 86 | +if (isset($_GET['code'])) { |
| 87 | + $client->authenticate($_GET['code']); |
| 88 | + $_SESSION['access_token'] = $client->getAccessToken(); |
| 89 | + $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; |
| 90 | + header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Set client access token. |
| 95 | + */ |
| 96 | +if (isset($_SESSION['access_token'])) { |
| 97 | + $client->setAccessToken($_SESSION['access_token']); |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * If all authentication has been successfully completed, make Google |
| 102 | + * Cloud Storage API requests. |
| 103 | + */ |
| 104 | +if ($client->getAccessToken()) { |
| 105 | + /** |
| 106 | + * Google Cloud Storage API request to retrieve the list of buckets in your |
| 107 | + * Google Cloud Storage project. |
| 108 | + */ |
| 109 | + $buckets = $storageService->buckets->listBuckets(DEFAULT_PROJECT); |
| 110 | + $listBucketsMarkup = generateMarkup('List Buckets', $buckets); |
| 111 | + |
| 112 | + /** |
| 113 | + * Google Cloud Storage API request to retrieve the list of objects in your |
| 114 | + * Google Cloud Storage bucket. |
| 115 | + */ |
| 116 | + $objects = $storageService->objects->listObjects(DEFAULT_BUCKET); |
| 117 | + $listObjectsMarkup = generateMarkup('List Objects', $objects); |
| 118 | + |
| 119 | + /** |
| 120 | + * Google Cloud Storage API request to retrieve the list of Access Control |
| 121 | + * Lists on your Google Cloud Storage buckets. |
| 122 | + */ |
| 123 | + $bucketsAccessControls = $storageService->bucketAccessControls-> |
| 124 | + listBucketAccessControls(DEFAULT_BUCKET); |
| 125 | + $listBucketsAccessControlsMarkup = generateMarkup( |
| 126 | + 'List Buckets Access Controls', $bucketsAccessControls); |
| 127 | + |
| 128 | + /** |
| 129 | + * Google Cloud Storage API request to retrieve the list of Access Control |
| 130 | + * Lists on your Google Cloud Storage objects. |
| 131 | + */ |
| 132 | + $objectsAccessControls = $storageService->objectAccessControls-> |
| 133 | + listObjectAccessControls(DEFAULT_BUCKET, DEFAULT_OBJECT); |
| 134 | + $listObjectsAccessControlsMarkup = generateMarkup( |
| 135 | + 'List Objects Access Controls', $objectsAccessControls); |
| 136 | + |
| 137 | + /** |
| 138 | + * Google Cloud Storage API request to retrieve a bucket from your |
| 139 | + * Google Cloud Storage project. |
| 140 | + */ |
| 141 | + $bucket = $storageService->buckets->get(DEFAULT_BUCKET); |
| 142 | + $getBucketMarkup = generateMarkup('Get Bucket', $bucket); |
| 143 | + |
| 144 | + // The access token may have been updated lazily. |
| 145 | + $_SESSION['access_token'] = $client->getAccessToken(); |
| 146 | +} else { |
| 147 | + $authUrl = $client->createAuthUrl(); |
| 148 | +} |
| 149 | +?> |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + Google Cloud Storage Sample App |
| 157 | + |
| 158 | + if(isset($listBucketsMarkup)): ?> |
| 159 | + |
| 160 | + print $listBucketsMarkup ?> |
| 161 | + |
| 162 | + endif ?> |
| 163 | + |
| 164 | + if(isset($listObjectsMarkup)): ?> |
| 165 | + |
| 166 | + print $listObjectsMarkup ?> |
| 167 | + |
| 168 | + endif ?> |
| 169 | + |
| 170 | + if(isset($listBucketsAccessControlsMarkup)): ?> |
| 171 | + |
| 172 | + print $listBucketsAccessControlsMarkup ?> |
| 173 | + |
| 174 | + endif ?> |
| 175 | + |
| 176 | + if(isset($listObjectsAccessControlsMarkup)): ?> |
| 177 | + |
| 178 | + print $listObjectsAccessControlsMarkup ?> |
| 179 | + |
| 180 | + endif ?> |
| 181 | + |
| 182 | + if(isset($getBucketMarkup)): ?> |
| 183 | + |
| 184 | + print $getBucketMarkup ?> |
| 185 | + |
| 186 | + endif ?> |
| 187 | + |
| 188 | + |
| 189 | + if(isset($authUrl)) { |
| 190 | + |
| 191 | + } else { |
| 192 | + |
| 193 | + } |
| 194 | + ?> |
| 195 | + |
| 196 | + |
| 197 | + |
0 commit comments