Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
130 lines (99 sloc) 4.14 KB
'''
Created on Dec 17, 2013
Shows how to perform bucket operations.
@version: 1.0
@author: mielem@gmail.com
'''
import boto
import time
# URI scheme for Google Cloud Storage.
GOOGLE_STORAGE = "gs"
def list_objects(bucket_name, debug_level):
'''
Performs a GET Bucket operation to list the objects contained in
the specified bucket.
@param bucket_name: The name of the bucket that contains the objects to list.
@param debug_level: The level of debug messages to be printed.
'''
try:
# Define the bucket URI
uri = boto.storage_uri(bucket_name, GOOGLE_STORAGE, debug_level)
# Get the specified bucket.
bucket = uri.get_bucket()
# Iterate through the objects in the bucket.
for obj in bucket:
print "%s://%s/%s" % (uri.scheme, uri.bucket_name, obj.name)
# print ' "%s"' % obj.get_contents_as_string()
except boto.exception, e:
logging.error("list_objects, error occurred: %s", e)
def create_bucket(bucket_name, project_id, debug_level):
'''
Performs a PUT Bucket operation to create the specified bucket in
the specified project.
@param bucket_name: The name of the bucket to create.
@param project_id: The id of the project that contains the new bucket.
@param debug_level: The level of debug messages to be printed.
'''
try:
# Create a unique name.
# For testing purposes only.
# Comment out the following two lines in a production application.
now = time.time()
new_bucket_name = bucket_name + "_%d" % now
# Define the bucket URI
uri = boto.storage_uri(new_bucket_name, GOOGLE_STORAGE, debug_level)
# Define the header values.
header_values = {"x-goog-api-version": "2",
"x-goog-project-id": str(project_id)}
uri.create_bucket(headers=header_values)
print 'Successfully created bucket "%s"' % new_bucket_name
return new_bucket_name
except boto.exception, e:
logging.error("create_bucket, error occurred: %s", e)
def create_dra_bucket(bucket_name, project_id, debug_level):
'''
Performs a PUT Bucket operation to create the specified bucket with
durable reduced availability in the specified project.
@param bucket_name: The name of the bucket to create.
@param project_id: The id of the project that contains the new bucket.
@param debug_level: The level of debug messages to be printed.
'''
try:
# Create a unique name.
# For testing purposes only.
# Comment out the following two lines in a production application.
now = time.time()
new_bucket_name = bucket_name + "_%d" % now
# Define the bucket URI
uri = boto.storage_uri(new_bucket_name, GOOGLE_STORAGE, debug_level)
# Define the header values.
header_values = {"x-goog-api-version": "2",
"x-goog-project-id": str(project_id)}
uri.create_bucket(headers=header_values, location="US",
storage_class="DURABLE_REDUCED_AVAILABILITY")
print 'Successfully created bucket "%s"' % new_bucket_name
return new_bucket_name
except boto.exception, e:
logging.error("create_dra_bucket, error occurred: %s", e)
def delete_bucket(bucket_name, debug_level):
'''
Performs a DELETE Bucket operation.
A bucket must be empty before it can be deleted, so its objects are
deleted first.
@param bucket_name: The name of the bucket to delete.
@param debug_level: The level of debug messages to be printed.
'''
try:
# Define the bucket URI
uri = boto.storage_uri(bucket_name, GOOGLE_STORAGE, debug_level)
# Get the specified bucket.
bucket = uri.get_bucket()
# Iterate through the objects in the bucket to delete them.
for obj in bucket:
print "Deleting object: %s..." % obj.name
obj.delete()
# Delete the bucket.
print "Deleting bucket: %s..." % uri.bucket_name
uri.delete_bucket()
except boto.exception, e:
logging.error("delete_bucket, error occurred: %s", e)