Skip to content

Navigation Menu

Sign in
Appearance settings
    • GitHub Copilot
      Write better code with AI
    • GitHub Models New
      Manage and compare prompts
    • GitHub Advanced Security
      Find and fix vulnerabilities
    • Actions
      Automate any workflow
    • Codespaces
      Instant dev environments
    • Issues
      Plan and track work
    • Code Review
      Manage code changes
    • Discussions
      Collaborate outside of code
    • Code Search
      Find more, search less
    Explore
    • Why GitHub
    • All features
    • Documentation
    • GitHub Skills
    • Blog
  • By company size
    • Enterprises
    • Small and medium teams
    • Startups
    • Nonprofits
    By use case
    • DevSecOps
    • DevOps
    • CI/CD
    • View all use cases
    By industry
    • Healthcare
    • Financial services
    • Manufacturing
    • Government
    • View all industries
    View all solutions
  • Topics
    • AI
    • DevOps
    • Security
    • Software Development
    • View all
    Explore
    • Learning Pathways
    • Events & Webinars
    • Ebooks & Whitepapers
    • Customer Stories
    • Partners
    • Executive Insights
    • GitHub Sponsors
      Fund open source developers
    • The ReadME Project
      GitHub community articles
    Repositories
    • Topics
    • Trending
    • Collections
    • Enterprise platform
      AI-powered developer platform
    Available add-ons
    • GitHub Advanced Security
      Enterprise-grade security features
    • Copilot for business
      Enterprise-grade AI features
    • Premium Support
      Enterprise-grade 24/7 support
  • Pricing

Search code, repositories, users, issues, pull requests...

Clear
    Search syntax tips

    Provide feedback

    We read every piece of feedback, and take your input very seriously.

    Saved searches

    Use saved searches to filter your results more quickly

    To see all available qualifiers, see our documentation.

    Sign in
    Sign up
    Appearance settings
    You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert
    php-func / php-docs-samples Public
    forked from GoogleCloudPlatform/php-docs-samples
    • Notifications You must be signed in to change notification settings
    • Fork 0
    • Star 0
    • Code
    • Pull requests 0
    • Actions
    • Projects 0
    • Security

      Uh oh!

      There was an error while loading. Please reload this page.

    • Insights
    Additional navigation options
    • Code
    • Pull requests
    • Actions
    • Projects
    • Security
    • Insights

    Commit b778261

    Browse filesBrowse files
    bshafferbshaffer
    committed
    Merge pull request GoogleCloudPlatform#2 from bshaffer/add-storage-samples
    Add storage samples
    2 parents 57b3c54 + 08596f8 commit b778261
    Copy full SHA for b778261

    File tree

    Expand file treeCollapse file tree

    2 files changed

    +244
    -0
    lines changed
    Filter options
    • storage
      • README.md
      • app.php
    Expand file treeCollapse file tree

    2 files changed

    +244
    -0
    lines changed

    ‎storage/README.md

    Copy file name to clipboard
    +47Lines changed: 47 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,47 @@
    1+
    # Google Cloud Storage PHP Sample Application
    2+
    3+
    ## Description
    4+
    This is a simple web-based example of calling the Google Cloud Storage API in
    5+
    PHP.
    6+
    7+
    ## Prerequisites:
    8+
    Please make sure that all of the following is installed before trying to run
    9+
    the sample application.
    10+
    11+
    - PHP 5.2.x or higher [http://www.php.net/]
    12+
    - PHP Curl extension [http://www.php.net/manual/en/intro.curl.php]
    13+
    - PHP JSON extension [http://php.net/manual/en/book.json.php]
    14+
    - The google-api-php-client library checked out locally
    15+
    16+
    ## Setup Authentication
    17+
    NOTE: This README assumes that you have enabled access to the Google Cloud
    18+
    Engine API via the Google API Console page.
    19+
    20+
    1) Visit https://console.developers.google.com to register your
    21+
    application and generate an API key.
    22+
    - Click **APIs & Auth** in the left column, and then click **Credentials**.
    23+
    - Click **Create new client ID** to create a new client ID.
    24+
    - In the **Create Client ID** window, choose **Web application**.
    25+
    - In the **Redirect URIs** box, specify the URL for your PHP page, e.g., http://localhost/app.php.
    26+
    - Click **Create Client ID**.
    27+
    - In the **Public API access** section of the **Credentials** page, click **Create new Key**.
    28+
    - In the **Create a new Key** window, choose **Browser key**.
    29+
    30+
    2) Update app.php with the redirect uri, consumer key, secret, and developer
    31+
    key obtained in step 1.
    32+
    - Update `YOUR_CLIENT_ID` with your oauth2 client id.
    33+
    - Update `YOUR_CLIENT_SECRET` with your oauth2 client secret.
    34+
    - Update `YOUR_REDIRECT_URI` with the fully qualified redirect URI, e.g., http://localhost/app.php.
    35+
    - Update `YOUR_API_KEY` with your API key.
    36+
    - Update `YOUR_PROJECT_DEFAULT_ID` with your project ID, which
    37+
    can be found by visiting https://console.developers.google.com and selecting a project.
    38+
    39+
    3) Update app.php with remaining default settings. Search and replace all
    40+
    strings starting with 'YOUR_DEFAULT_' with their associated values.
    41+
    42+
    ## Running the Sample Application
    43+
    4) Load app.php on your web server, and visit the appropriate website in
    44+
    your web browser.
    45+
    46+
    ## More Information
    47+
    See [https://developers.google.com/storage/docs/json_api/v1/json-api-php-samples](https://developers.google.com/storage/docs/json_api/v1/json-api-php-samples)

    ‎storage/app.php

    Copy file name to clipboard
    +197Lines changed: 197 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,197 @@
    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+
    $apiRequestMarkup .= "
    ";
    61+
    $apiRequestMarkup .= print_r(json_decode(json_encode($apiResponse), true),
    62+
    true);
    63+
    $apiRequestMarkup .= "";
    64+
    } else {
    65+
    foreach($apiResponse['items'] as $response) {
    66+
    $apiRequestMarkup .= "
    ";
    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+
    print "Authorize Me!";
    191+
    } else {
    192+
    print "Logout";
    193+
    }
    194+
    ?>
    195+
    196+
    197+

    0 commit comments

    Comments
    0 (0)

    Footer

    © 2025 GitHub, Inc.

    Footer navigation

    • Terms
    • Privacy
    • Security
    • Status
    • Docs
    • Contact
    You can’t perform that action at this time.