This post demonstrates how to build a Google Cloud Storage XML API Python application. The application interacts with the storage service via the XML API (and the httplib2 library). It uses a simple interface which allows you to perform tasks such as: list the buckets in a project, list objects in a bucket, create a bucket, create an object and so on. The intent of the application is educational. It should help you to understand the API syntax (and semantics) when interacting with Google Cloud Storage.
Prerequisites
To build the application, you will use Eclipse as shown next. Before you can perform the next steps, assure that you have satisfied the requirements described here: Google Cloud Storage Python Applications Prerequisites.
Also make sure to satisfy the following :
- Install the required software as listed next:
- Update the information contained in the client_secrets.json file. Use your client id and secret available in the Google API Console.
Create the Application Project
- Activate Eclipse.
- From the File menu, select New->PyDevProject. The PyDev Project window is displayed.
- In the Project name: enter PythonXmlApi.
- Accept the user default workspace; for example: /Users/[your username]/[your work directory]/PythonXmlApi. This is the directory where the program will be stored.
- If you have more than one Python version installed perform the following steps:
- In the Grammar Version selection list select 2.7.
- In the Interpreter selection list select the desired Python interpreter. For more information, see Configure PyDev.
- Click Finish. The PythonXmlApi project is created and displayed in the Package Explorer.
- If the PyDev perspective is not selected yet, click on the Open Perspective icon in the upper right corner of Eclipse. From the pop-up menu select PyDev and click OK.
- In the Package Explorer, expand the PythonXmlApi project node.
- Right-click the src folder.
- Select New->PyDev Module.
- In the pop-up dialog window enter the following information:
- In the Package box enter gcs_examples.
- In the Name box enter main.
- Click Finish.
- Select Module Main and click OK.
An empty main.py is created. Actually the module contains the standard Python check for main because the template chosen. You’ll enter the code needed to interact with Google Cloud Storage in the next section.
Download the Application Code
You can download the archived project from this location: storage-xmlapi-python-client. Then import it into Eclipse. The project contains the following modules:
- main.py. This is the main entry point of the application which allows you to
interact with Google Cloud Storage (GCS) using XML API. It contains the main function which is called when the application is executed.
-
simple_ui.py. Contains the GCS_SimpleUI class which provides a simple UI to interact with Google Cloud Storage.
- commands.py. Contains the GCS_Command class which provides the entry point for
processing user’s selection. Based on the user’s selection, the related Google Cloud Storageoperation is executed.
-
authentication.py. It contains the class that generates an authenticated HTTP client object.
-
bucket_commands.py. Contains the GCS_Bucket class which handles Google Cloud Storage bucket operations.
-
object_commands.py. Contains the GCS_Object class which handles Google Cloud Storage object operations.
- config.py. Contains data shared by all the modules.
PythonXmlApi Implementation
The PythonXmlApi main function performs the preliminary initialization and starts the application. A set of classes then perform the actual tasks such as: creating a simple UI, processing user’s input, interacting with the storage service and so on. The following picture shows the classes and their hierarchy.
PythonXmlApi Workflow
The first time you start the application, the main function instantiates GCS_SimpleUI and GCS_Command classes then initializes a simple user interface which accepts the user’s input. You will be asked to authenticate the application which uses OAuth2.0 and stores the credentials in a local file called stored_credentials.json. Also, you will be asked to enter the project ID which will be stored in a local file called project.dat.
Every time you make a selection a command call is issued which in turn calls a GCS_Bucket or GCS_Object method. This method executes the actual Google Cloud Storage bucket or object request. The storage service’s response is then displayed for your information. If the request fails, an error is displayed. Finally, depending on the debugging level, the application displays what goes on the “wire”. This is to help you understand the actual HTTP request and response content as explained by the Google Cloud Storage documentation Reference Methods. The following picture depicts the workflow just described.
Usage
In a terminal window activate the program as follows:
python main.py --logging_level [DEBUG | INFO | WARNING | ERROR | CRITICAL]
Have Fun!!
I have to thank you for the efforts you have put in writing this
website. I am hoping to see the same high-grade content by you later on as
well. In truth, your creative writing abilities has encouraged me to get my very
own website now 😉