Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Add GCS versioning support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaniv Ovadia committed Nov 26, 2012
1 parent 348b1e3 commit 1a0db44
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 50 deletions.
158 changes: 147 additions & 11 deletions boto/gs/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from boto.exception import InvalidAclError
from boto.gs.acl import ACL, CannedACLStrings
from boto.gs.acl import SupportedPermissions as GSPermissions
from boto.gs.bucketlistresultset import VersionedBucketListResultSet
from boto.gs.cors import Cors
from boto.gs.key import Key as GSKey
from boto.s3.acl import Policy
Expand All @@ -36,6 +37,9 @@
CORS_ARG = 'cors'

class Bucket(S3Bucket):
VersioningBody = ('<?xml version="1.0" encoding="UTF-8"?>\n'
'<VersioningConfiguration><Status>%s</Status>'
'</VersioningConfiguration>')
WebsiteBody = ('<?xml version="1.0" encoding="UTF-8"?>\n'
'<WebsiteConfiguration>%s%s</WebsiteConfiguration>')
WebsiteMainPageFragment = '<MainPageSuffix>%s</MainPageSuffix>'
Expand All @@ -44,18 +48,116 @@ class Bucket(S3Bucket):
def __init__(self, connection=None, name=None, key_class=GSKey):
super(Bucket, self).__init__(connection, name, key_class)

def set_acl(self, acl_or_str, key_name='', headers=None, version_id=None):
"""sets or changes a bucket's or key's acl (depending on whether a
key_name was passed). We include a version_id argument to support a
polymorphic interface for callers, however, version_id is not relevant
for Google Cloud Storage buckets and is therefore ignored here."""
def get_key(self, key_name, headers=None, version_id=None,
response_headers=None, generation=None):
"""
Check to see if a particular key exists within the bucket. This
method uses a HEAD request to check for the existance of the key.
Returns: An instance of a Key object or None
:type key_name: string
:param key_name: The name of the key to retrieve
:type response_headers: dict
:param response_headers: A dictionary containing HTTP
headers/values that will override any headers associated
with the stored object in the response. See
http://goo.gl/06N3b for details.
:rtype: :class:`boto.s3.key.Key`
:returns: A Key object from this bucket.
"""
query_args_l = []
if generation:
query_args_l.append('generation=%s' % generation)
if response_headers:
for rk, rv in response_headers.iteritems():
query_args_l.append('%s=%s' % (rk, urllib.quote(rv)))

key, resp = self._get_key_internal(key_name, headers,
query_args_l=query_args_l)
if key:
key.meta_generation = resp.getheader('x-goog-meta-generation')
key.generation = resp.getheader('x-goog-generation')
return key

def copy_key(self, new_key_name, src_bucket_name, src_key_name,
metadata=None, src_version_id=None, storage_class='STANDARD',
preserve_acl=False, encrypt_key=False, headers=None,
query_args=None, src_generation=None):
if src_generation:
headers['x-goog-copy-source-generation'] = src_generation
super(Bucket, self).copy_key(new_key_name, src_bucket_name,
src_key_name, metadata=metadata,
storage_class=storage_class,
preserve_acl=preserve_acl,
encrypt_key=encrypt_key, headers=headers,
query_args=query_args)

def list_versions(self, prefix='', delimiter='', marker='',
generation_marker='', headers=None):
"""
List versioned objects within a bucket. This returns an
instance of an VersionedBucketListResultSet that automatically
handles all of the result paging, etc. from GCS. You just need
to keep iterating until there are no more results. Called
with no arguments, this will return an iterator object across
all keys within the bucket.
:type prefix: string
:param prefix: allows you to limit the listing to a particular
prefix. For example, if you call the method with
prefix='/foo/' then the iterator will only cycle through
the keys that begin with the string '/foo/'.
:type delimiter: string
:param delimiter: can be used in conjunction with the prefix
to allow you to organize and browse your keys
hierarchically. See:
https://developers.google.com/storage/docs/reference-headers#delimiter
for more details.
:type marker: string
:param marker: The "marker" of where you are in the result set
:type generation_marker: string
:param marker: The "generation marker" of where you are in the result
set
:rtype:
:class:`boto.gs.bucketlistresultset.VersionedBucketListResultSet`
:return: an instance of a BucketListResultSet that handles paging, etc
"""
return VersionedBucketListResultSet(self, prefix, delimiter,
marker, generation_marker,
headers)

def delete_key(self, key_name, headers=None, version_id=None,
mfa_token=None, generation=None):
query_args_l = []
if generation:
query_args_l.append('generation=%s' % generation)
self._delete_key_internal(key_name, headers=headers,
version_id=version_id, mfa_token=mfa_token,
query_args_l=query_args_l)

def set_acl(self, acl_or_str, key_name='', headers=None, version_id=None,
generation=None):
"""Sets or changes a bucket's or key's ACL. The generation argument can
be used to specify an object version, else we will modify the current
version."""
key_name = key_name or ''
query_args = STANDARD_ACL
if generation:
query_args += '&generation=%d' % generation
if isinstance(acl_or_str, Policy):
raise InvalidAclError('Attempt to set S3 Policy on GS ACL')
elif isinstance(acl_or_str, ACL):
self.set_xml_acl(acl_or_str.to_xml(), key_name, headers=headers)
self.set_xml_acl(acl_or_str.to_xml(), key_name, headers=headers,
query_args=query_args)
else:
self.set_canned_acl(acl_or_str, key_name, headers=headers)
self.set_canned_acl(acl_or_str, key_name, headers=headers,
generation=generation)

def set_def_acl(self, acl_or_str, key_name='', headers=None):
"""sets or changes a bucket's default object acl. The key_name argument
Expand All @@ -82,12 +184,16 @@ def get_acl_helper(self, key_name, headers, query_args):
raise self.connection.provider.storage_response_error(
response.status, response.reason, body)

def get_acl(self, key_name='', headers=None, version_id=None):
def get_acl(self, key_name='', headers=None, version_id=None,
generation=None):
"""returns a bucket's acl. We include a version_id argument
to support a polymorphic interface for callers, however,
version_id is not relevant for Google Cloud Storage buckets
and is therefore ignored here."""
return self.get_acl_helper(key_name, headers, STANDARD_ACL)
query_args = STANDARD_ACL
if generation:
query_args += '&generation=%d' % generation
return self.get_acl_helper(key_name, headers, query_args)

def get_def_acl(self, key_name='', headers=None):
"""returns a bucket's default object acl. The key_name argument is
Expand All @@ -112,13 +218,16 @@ def set_canned_acl_helper(self, acl_str, key_name, headers, query_args):
response.status, response.reason, body)

def set_canned_acl(self, acl_str, key_name='', headers=None,
version_id=None):
version_id=None, generation=None):
"""sets or changes a bucket's acl to a predefined (canned) value.
We include a version_id argument to support a polymorphic
interface for callers, however, version_id is not relevant for
Google Cloud Storage buckets and is therefore ignored here."""
query_args = STANDARD_ACL
if generation:
query_args += '&generation=%d' % generation
return self.set_canned_acl_helper(acl_str, key_name, headers,
STANDARD_ACL)
query_args=query_args)

def set_def_canned_acl(self, acl_str, key_name='', headers=None):
"""sets or changes a bucket's default object acl to a predefined
Expand Down Expand Up @@ -386,3 +495,30 @@ def get_website_configuration_with_xml(self, headers=None):

def delete_website_configuration(self, headers=None):
self.configure_website(headers=headers)

def get_versioning_status(self, headers=None):
"""
Returns the current status of versioning configuration on the bucket.
:rtype: boolean
:returns: boolean indicating whether or not versioning is enabled.
"""
response = self.connection.make_request('GET', self.name,
query_args='versioning',
headers=headers)
body = response.read()
boto.log.debug(body)
if response.status != 200:
raise self.connection.provider.storage_response_error(
response.status, response.reason, body)
resp_json = boto.jsonresponse.Element()
boto.jsonresponse.XmlHandler(resp_json, None).parse(body)
resp_json = resp_json['VersioningConfiguration']
return ('Status' in resp_json) and (resp_json['Status'] == 'Enabled')

def configure_versioning(self, enabled, headers=None):
if enabled == True:
req_body = self.VersioningBody % ('Enabled')
else:
req_body = self.VersioningBody % ('Suspended')
self.set_subresource('versioning', req_body, headers=headers)
64 changes: 64 additions & 0 deletions boto/gs/bucketlistresultset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2012 Google Inc.
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

def versioned_bucket_lister(bucket, prefix='', delimiter='',
marker='', generation_marker='', headers=None):
"""
A generator function for listing versioned objects.
"""
more_results = True
k = None
while more_results:
rs = bucket.get_all_versions(prefix=prefix, marker=marker,
generation_marker=generation_marker,
delimiter=delimiter, headers=headers,
max_keys=999)
for k in rs:
yield k
marker = rs.next_marker
generation_marker = rs.next_generation_marker
more_results= rs.is_truncated

class VersionedBucketListResultSet:
"""
A resultset for listing versions within a bucket. Uses the bucket_lister
generator function and implements the iterator interface. This
transparently handles the results paging from GCS so even if you have
many thousands of keys within the bucket you can iterate over all
keys in a reasonably efficient manner.
"""

def __init__(self, bucket=None, prefix='', delimiter='', marker='',
generation_marker='', headers=None):
self.bucket = bucket
self.prefix = prefix
self.delimiter = delimiter
self.marker = marker
self.generation_marker = generation_marker
self.headers = headers

def __iter__(self):
return versioned_bucket_lister(self.bucket, prefix=self.prefix,
delimiter=self.delimiter,
marker=self.marker,
generation_marker=self.generation_marker,
headers=self.headers)
44 changes: 44 additions & 0 deletions boto/gs/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,50 @@
from boto.s3.key import Key as S3Key

class Key(S3Key):
generation = None
meta_generation = None

def endElement(self, name, value, connection):
if name == 'Key':
self.name = value
elif name == 'ETag':
self.etag = value
elif name == 'IsLatest':
if value == 'true':
self.is_latest = True
else:
self.is_latest = False
elif name == 'LastModified':
self.last_modified = value
elif name == 'Size':
self.size = int(value)
elif name == 'StorageClass':
self.storage_class = value
elif name == 'Owner':
pass
elif name == 'VersionId':
self.version_id = value
elif name == 'Generation':
self.generation = value
elif name == 'MetaGeneration':
self.meta_generation = value
else:
setattr(self, name, value)

def get_file(self, fp, headers=None, cb=None, num_cb=10,
torrent=False, version_id=None, override_num_retries=None,
response_headers=None):
query_args = None
if self.generation:
query_args = ['generation=%s' % self.generation]
self._get_file_internal(fp, headers=headers, cb=cb, num_cb=num_cb,
override_num_retries=override_num_retries,
response_headers=response_headers,
query_args=query_args)

def delete(self):
return self.bucket.delete_key(self.name, version_id=self.version_id,
generation=self.generation)

def add_email_grant(self, permission, email_address):
"""
Expand Down
2 changes: 0 additions & 2 deletions boto/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,3 @@ def endElement(self, name):

def characters(self, content):
self.current_text += content


4 changes: 3 additions & 1 deletion boto/resultset.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, marker_elem=None):
self.next_key_marker = None
self.next_upload_id_marker = None
self.next_version_id_marker = None
self.next_generation_marker= None
self.version_id_marker = None
self.is_truncated = False
self.next_token = None
Expand Down Expand Up @@ -94,6 +95,8 @@ def endElement(self, name, value, connection):
self.version_id_marker = value
elif name == 'NextVersionIdMarker':
self.next_version_id_marker = value
elif name == 'NextGenerationMarker':
self.next_generation_marker = value
elif name == 'UploadIdMarker':
self.upload_id_marker = value
elif name == 'NextUploadIdMarker':
Expand Down Expand Up @@ -164,4 +167,3 @@ def endElement(self, name, value, connection):
self.request_id = value
else:
setattr(self, name, value)

Loading

1 comment on commit 1a0db44

@tpodowd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi - can you comment on my pull request 1182. It fixes a bug introduced by this change. I didn't look at the gs implementation of delete_key to check if it also had the issue. Thanks!

Please sign in to comment.