Skip to content
Browse files

Changed BucketStorageURI to update properties from Key object when

set_contents_from_* functions are called.
  • Loading branch information...
1 parent 1fab6ee commit 291881806414f98b9462d0a0c4d3f2b25521815d @jterrace jterrace committed Jan 29, 2013
Showing with 60 additions and 13 deletions.
  1. +25 −13 boto/storage_uri.py
  2. +35 −0 tests/integration/gs/test_storage_uri.py
View
38 boto/storage_uri.py
@@ -297,6 +297,11 @@ def __init__(self, scheme, bucket_name=None, object_name=None,
self.meta_generation = meta_generation and int(meta_generation)
self.is_latest = is_latest
+ def _update_from_key(self, key):
+ self.version_id = getattr(key, 'version_id', None)
+ self.generation = getattr(key, 'generation', None)
+ self.meta_generation = getattr(key, 'meta_generation', None)
+
def get_key(self, validate=False, headers=None, version_id=None):
self._check_object_uri('get_key')
bucket = self.get_bucket(validate, headers)
@@ -599,35 +604,42 @@ def set_contents_from_string(self, s, headers=None, replace=True,
sys.stderr.write('Warning: GCS does not support '
'reduced_redundancy; argument ignored by '
'set_contents_from_string')
- key.set_contents_from_string(s, headers, replace, cb, num_cb,
- policy, md5)
+ result = key.set_contents_from_string(
+ s, headers, replace, cb, num_cb, policy, md5)
else:
- key.set_contents_from_string(s, headers, replace, cb, num_cb,
- policy, md5, reduced_redundancy)
+ result = key.set_contents_from_string(
+ s, headers, replace, cb, num_cb, policy, md5,
+ reduced_redundancy)
+ self._update_from_key(key)
+ return result
def set_contents_from_file(self, fp, headers=None, replace=True, cb=None,
num_cb=10, policy=None, md5=None, size=None,
rewind=False, res_upload_handler=None):
self._check_object_uri('set_contents_from_file')
key = self.new_key(headers=headers)
if self.scheme == 'gs':
- return key.set_contents_from_file(
- fp, headers, replace, cb, num_cb, policy, md5, size=size,
- rewind=rewind, res_upload_handler=res_upload_handler)
+ result = key.set_contents_from_file(
+ fp, headers, replace, cb, num_cb, policy, md5, size=size,
+ rewind=rewind, res_upload_handler=res_upload_handler)
else:
self._warn_about_args('set_contents_from_file',
res_upload_handler=res_upload_handler)
- return key.set_contents_from_file(fp, headers, replace, cb, num_cb,
- policy, md5, size=size,
- rewind=rewind)
+ result = key.set_contents_from_file(
+ fp, headers, replace, cb, num_cb, policy, md5, size=size,
+ rewind=rewind)
+ self._update_from_key(key)
+ return result
def set_contents_from_stream(self, fp, headers=None, replace=True, cb=None,
policy=None, reduced_redundancy=False):
self._check_object_uri('set_contents_from_stream')
dst_key = self.new_key(False, headers)
- dst_key.set_contents_from_stream(fp, headers, replace, cb,
- policy=policy,
- reduced_redundancy=reduced_redundancy)
+ result = dst_key.set_contents_from_stream(
+ fp, headers, replace, cb, policy=policy,
+ reduced_redundancy=reduced_redundancy)
+ self._update_from_key(dst_key)
+ return result
def copy_key(self, src_bucket_name, src_key_name, metadata=None,
src_version_id=None, storage_class='STANDARD',
View
35 tests/integration/gs/test_storage_uri.py
@@ -23,6 +23,8 @@
"""Unit tests for StorageUri interface."""
+import StringIO
+
from boto import storage_uri
from tests.integration.gs.testcase import GSTestCase
@@ -80,3 +82,36 @@ def testCloneReplaceKey(self):
self.assertTrue(uri.has_version())
self.assertRegexpMatches(str(uri.generation), r'[0-9]+')
self.assertEquals(uri.meta_generation, 1)
+
+ def testPropertiesUpdated(self):
+ b = self._MakeBucket()
+ bucket_uri = storage_uri("gs://%s" % b.name)
+ key_uri = bucket_uri.clone_replace_name("obj")
+ key_uri.set_contents_from_string("data1")
+
+ self.assertRegexpMatches(str(key_uri.generation), r"[0-9]+")
+ self.assertEquals(int(key_uri.meta_generation), 1)
+ k = b.get_key("obj")
+ self.assertEqual(k.generation, key_uri.generation)
+ self.assertEqual(k.meta_generation, key_uri.meta_generation)
+ self.assertEquals(k.get_contents_as_string(), "data1")
+
+ key_uri.set_contents_from_stream(StringIO.StringIO("data2"))
+ self.assertRegexpMatches(str(key_uri.generation), r"[0-9]+")
+ self.assertGreater(key_uri.generation, k.generation)
+ self.assertEqual(int(key_uri.meta_generation), 1)
+ k = b.get_key("obj")
+ self.assertEqual(k.generation, key_uri.generation)
+ self.assertEqual(k.meta_generation, key_uri.meta_generation)
+ self.assertEquals(int(key_uri.meta_generation), 1)
+ self.assertEquals(k.get_contents_as_string(), "data2")
+
+ key_uri.set_contents_from_file(StringIO.StringIO("data3"))
+ self.assertRegexpMatches(str(key_uri.generation), r"[0-9]+")
+ self.assertGreater(key_uri.generation, k.generation)
+ self.assertEqual(int(key_uri.meta_generation), 1)
+ k = b.get_key("obj")
+ self.assertEqual(k.generation, key_uri.generation)
+ self.assertEqual(k.meta_generation, key_uri.meta_generation)
+ self.assertEquals(int(key_uri.meta_generation), 1)
+ self.assertEquals(k.get_contents_as_string(), "data3")

0 comments on commit 2918818

Please sign in to comment.
Something went wrong with that request. Please try again.