Using the Cloud Client Libraries for Python

This document demonstrates how to use the Cloud Client Libraries for Python for Compute Engine. It describes how to authorize requests and how to create, list, and delete instances. This exercise discusses how to use the google-api-python-client library to access Compute Engine resources. You can run this sample from your local machine or on a VM instance, provided that you have authorized the sample correctly.

For a full list of available client libraries, including other Google client libraries and third-party open source libraries, see the client libraries page.

To view the full code example with all of the necessary imports, visit the GoogleCloudPlatform/python-docs-samples GitHub page.

Objectives

  • Perform OAuth 2.0 authorization using the oauth2client library
  • Create an instance using the google-python-client library
  • List instances using the google-python-client library
  • Delete an instance using the google-python-client library

Costs

This tutorial uses billable components of Google Cloud including Compute Engine.

New Google Cloud users might be eligible for a free trial.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  6. Install the Cloud SDK.
  7. After the SDK is installed, run gcloud auth application-default login.
  8. Install the google-api-python-client library. Typically, you can run:
    $ pip install --upgrade google-api-python-client

    For more information about how to install this library, see the installation instructions. You also need to have Python 2.7 or 3.3+ to run the Cloud Client Libraries for Python.

  9. Enable the Cloud Storage API.
  10. Create a Cloud Storage bucket and note the bucket name for later.

Authorizing requests

This sample uses OAuth 2.0 authorization. There are many ways to authorize requests using OAuth 2.0, but for the example use application default credentials. This lets you reuse the credentials from the gcloud tool if you are running the sample on a local workstation or reuse credentials from a service account if you are running the sample from within Compute Engine or App Engine. You should have installed and authorized the gcloud tool in the Before you begin section.

Application default credentials are provided in Google API Client Libraries automatically. You just have to build and initialize the API:

compute = googleapiclient.discovery.build('compute', 'v1')

For example, the following snippet is the main method of this sample, which builds and initializes the API and then makes some calls to create, list, and delete an instance: