Skip to content

Commit

Permalink
Fetch key storage class on-demand.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Jul 14, 2014
1 parent 5a28d1c commit f3e5d11
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 18 additions & 1 deletion boto/s3/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __init__(self, bucket=None, name=None):
self.is_latest = False
self.last_modified = None
self.owner = None
self.storage_class = 'STANDARD'
self._storage_class = None
self.path = None
self.resp = None
self.mode = None
Expand Down Expand Up @@ -187,6 +187,23 @@ def _set_base64md5(self, value):

base64md5 = property(_get_base64md5, _set_base64md5);

def _get_storage_class(self):
if self._storage_class is None and self.bucket:
# Attempt to fetch storage class
list_items = list(self.bucket.list(self.name.encode('utf-8')))
if len(list_items):
self._storage_class = list_items[0].storage_class
else:
# Key is not yet saved? Just use default...
self._storage_class = 'STANDARD'

return self._storage_class

def _set_storage_class(self, value):
self._storage_class = value

storage_class = property(_get_storage_class, _set_storage_class)

def get_md5_from_hexdigest(self, md5_hexdigest):
"""
A utility function to create the 2-tuple (md5hexdigest, base64md5)
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/s3/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def test_500_retry(self, sleep_mock):
self.set_http_response(status_code=500)
b = Bucket(self.service_connection, 'mybucket')
k = b.new_key('test_failure')
k.storage_class = 'STANDARD'
fail_file = StringIO('This will attempt to retry.')

with self.assertRaises(BotoServerError):
Expand All @@ -103,6 +104,7 @@ def test_400_timeout(self, sleep_mock):
self.set_http_response(status_code=400, body=weird_timeout_body)
b = Bucket(self.service_connection, 'mybucket')
k = b.new_key('test_failure')
k.storage_class = 'STANDARD'
fail_file = StringIO('This will pretend to be chunk-able.')

k.should_retry = counter(k.should_retry)
Expand All @@ -119,6 +121,7 @@ def test_502_bad_gateway(self, sleep_mock):
self.set_http_response(status_code=502, body=weird_timeout_body)
b = Bucket(self.service_connection, 'mybucket')
k = b.new_key('test_failure')
k.storage_class = 'STANDARD'
fail_file = StringIO('This will pretend to be chunk-able.')

k.should_retry = counter(k.should_retry)
Expand All @@ -135,6 +138,7 @@ def test_504_gateway_timeout(self, sleep_mock):
self.set_http_response(status_code=504, body=weird_timeout_body)
b = Bucket(self.service_connection, 'mybucket')
k = b.new_key('test_failure')
k.storage_class = 'STANDARD'
fail_file = StringIO('This will pretend to be chunk-able.')

k.should_retry = counter(k.should_retry)
Expand Down

0 comments on commit f3e5d11

Please sign in to comment.