Permalink
Browse files

Adds Support for More Range header variations to the read() Method fo…

…r Objects
  • Loading branch information...
sudorandom committed Oct 19, 2012
1 parent 444efe9 commit d7da21d15728f8d3c02efcccaac5793d85ba9a2c
Showing with 26 additions and 6 deletions.
  1. +11 −4 object_storage/storage_object.py
  2. +15 −2 tests/test_storage_object.py
@@ -225,7 +225,7 @@ def delete(self, recursive=False):
"""
return self.client.delete_object(self.container, self.name)
- def read(self, size=0, offset=0, headers=None):
+ def read(self, size=None, offset=None, headers=None):
""" Reads object content
@param size: number of bytes to read (0 reads all of the object data)
@@ -234,9 +234,16 @@ def read(self, size=0, offset=0, headers=None):
@return: str, data
"""
headers = headers or {}
- if size > 0:
- _range = 'bytes=%d-%d' % (offset, (offset + size) - 1)
- headers['Range'] = _range
+ if all([offset, size]):
+ end = (offset + size) - 1
+ headers['Range'] = 'bytes=%s-%s' % (offset, end)
+ elif offset is None and size is not None and size < 0:
+ headers['Range'] = 'bytes=%s' % (size,)
+ elif offset is None and size:
+ end = size - 1
+ headers['Range'] = 'bytes=0-%s' % (end,)
+ elif offset is not None and size is None:
+ headers['Range'] = 'bytes=%s-' % (offset,)
def _formatter(res):
return res.content
@@ -29,9 +29,22 @@ def test_read(self):
_result = Mock()
self.obj.make_request = Mock(return_value=_result)
result = self.obj.read()
+ self.obj.make_request.called_once_with('GET')
- result = self.obj.read(1111, 2222)
- self.obj.make_request.called_once_with('GET', headers={'Range': 'bytes=1111-3332'})
+ def test_read_with_offsets(self):
+ _result = Mock()
+ self.obj.make_request = Mock(return_value=_result)
+ result = self.obj.read(size=1111, offset=2222)
+ self.assertEqual(self.obj.make_request.call_args[1]['headers'], {'Range': 'bytes=2222-3332'})
+
+ result = self.obj.read(size=1111)
+ self.assertEqual(self.obj.make_request.call_args[1]['headers'], {'Range': 'bytes=0-1110'})
+
+ result = self.obj.read(size=-1111)
+ self.assertEqual(self.obj.make_request.call_args[1]['headers'], {'Range': 'bytes=-1111'})
+
+ result = self.obj.read(offset=2222)
+ self.assertEqual(self.obj.make_request.call_args[1]['headers'], {'Range': 'bytes=2222-'})
def test_copy_to(self):
_make_request = Mock()

0 comments on commit d7da21d

Please sign in to comment.