Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
226 lines (180 sloc) 7.38 KB
File Name:
Provides a simple user interface to exercise Google Cloud Storage XML API.
It uses the boto library bundled with the gsutil tool.
@version: 1.0
import logging
import shutil
import os
import sys
import gflags
from gslib.third_party.oauth2_plugin import oauth2_plugin
# Local imports
import Project
import Buckets
import Objects
# Bonus test functions
import Playpen
__author__ = " (Michael Miele)"
# User's choice global variable.
choice = ""
# Define the application's parameters the user must enter
# at the activation time.
FLAGS = gflags.FLAGS
# Define the project_id parameter
gflags.DEFINE_integer("project_id", None,
"Specify project to operate on", lower_bound=0)
# Define the bucket_name parameter
gflags.DEFINE_string("bucket_name", None,
"Specify bucket in the project to operate on")
# Define the oject_name parameter
gflags.DEFINE_string("object_name", None,
"Specify object in the bucket to operate on")
# Define the debug level parameter
gflags.DEFINE_integer("debug_level", None,
"Specify level of debug messages to be printed. 0 no messages, 1 basic messages, 2 all messages plus request and response.")
def menu():
Auxiliary function that creates the selection menu.
# Make sure to refer to the global choice variable.
# Remember that any time you use a variable in a function
# it automatically becomes local. Unless you explicitly
# declare it global.
global choice
if choice != "":
print "Enter to clear and get the menu."
menu_string = """
***** Project Operations *****
p1 -- GET Service -- List Buckets in a Project
***** Bucket Operations *****
b1 -- GET Bucket -- List Objects in a Bucket
b2 -- PUT Bucket -- Create a Bucket
b3 -- PUT Bucket -- Create Durable Reduced Availability bucket
b4 -- DELETE Bucket -- Delete a Bucket
***** Object Operations *****
o1 -- GET Object -- Get an Object ACLs
o2 -- GET Object -- Download an Object
o3 -- PUT Object -- Upload an Object
o4 -- PUT Object -- Copy an Object between buckets
o5 -- PUT Object -- Update an Object ACLs
o6 -- POST Object -- Upload an Object
o7 -- DELETE Object -- Delete an Object
o8 -- HEAD Object -- List an Object metadata
***** Testing Operations *****
t -- Test -- Test work in progress.
Make your selection. Click Enter to clear or X to exit.
# Display menu.
print menu_string
# Get user's choice.
choice = raw_input("\t")
return choice
def main(argv):
Main program entry point.
Sets global variables.
Defines the program arguments.
Displays menu and waits for user's input.
Prompts the user to enter the needed arguments.
Verifies that the user entered the required arguments.
Exits the program based on the user's input.
In a terminal window activate the program as follows:
python --project_id [your-project-id] --bucket_name [your-bucket-name]
--object_name [your-object-name]
--debug_level [0 no debug messages | 1 basic messages | 2 all messages plus request/response].
If the --debug_level is absent the default value of 0 is used.
# Initialize global variables.
global choice
# Set the choice switch to empty string.
choice = ""
new_bucket_name = None
# Read user's input parameters.
argv = FLAGS(argv)
except gflags.FlagsError, e:
print "%s\\nUsage: %s ARGS\\n%s" % (e, argv[0], FLAGS)
if not FLAGS.project_id:
print "A project id must be specified."
print "Usage: %s ARGS\\n%s" % (argv[0], FLAGS)
elif not FLAGS.bucket_name:
print "A bucket name must be specified."
elif not FLAGS.object_name:
print "A object name must be specified."
elif not FLAGS.debug_level:
# No debug level specified. Applied default value 0.
FLAGS.debug_level = 0
# Assign the project Id.
project_id = FLAGS.project_id
# Assign the bucket name.
bucket_name = FLAGS.bucket_name
# Assign the object name.
object_name = FLAGS.object_name
# Assign the debug_Level.
debug_level = FLAGS.debug_level
while choice != "x":
choice = menu().lower()
# Service operations.
if choice == "p1":
print "Execute GET Service. List buckets in the project."
Project.list_buckets(project_id, debug_level)
# Bucket operations.
elif choice == "b1":
print "Execute GET Bucket. List objects in a bucket."
Buckets.list_objects(bucket_name, debug_level)
elif choice == "b2":
print "Execute PUT Bucket. Create a bucket."
new_bucket_name = Buckets.create_bucket(bucket_name, project_id, debug_level)
elif choice == "b3":
print "Execute PUT Bucket. Create durable reduced availability bucket."
new_bucket_name = Buckets.create_dra_bucket(bucket_name, project_id, debug_level)
elif choice == "b4":
print "Execute DELETE Bucket. Delete the specified bucket."
if new_bucket_name is None:
print "\n Create the bucket first. Run selection b2.\n"
Buckets.delete_bucket(new_bucket_name, debug_level)
new_bucket_name = None
# Object operations.
elif choice == "o1":
print "Execute GET Object. Get object ACLs."
Objects.get_object_acls(bucket_name, object_name, debug_level)
elif choice == "o2":
print "Execute GET Object. Download object."
Objects.download_object(bucket_name, object_name, debug_level)
elif choice == "o3":
print "Execute PUT Object. Upload object."
Objects.upload_object(bucket_name, object_name, debug_level)
elif choice == "o4":
print "Execute PUT Object. Copy object between buckets."
Objects.copy_object(bucket_name, object_name, project_id, debug_level)
elif choice == "o5":
print "Execute PUT Object. Set object ACLs."
# Substitute the email value with your own.
Objects.set_object_acls(bucket_name, object_name, "READ", "your e-mail", debug_level)
elif choice == "o6":
print "Execute POST Object. Not implemented yet."
elif choice == "o7":
print "Execute DELETE Object. Delete object."
Objects.delete_object(bucket_name, object_name, debug_level)
elif choice == "o8":
print "Execute HEAD Object. List object metadata."
Objects.get_object_metadata(bucket_name, object_name, debug_level)
elif choice == "t":
if choice != "x":
print "Invalid Choice"
if __name__ == "__main__":