diff options
author | Stuart McLaren <stuart.mclaren@hp.com> | 2015-02-12 12:34:07 +0000 |
---|---|---|
committer | Alistair Coles <alistair.coles@hp.com> | 2015-03-24 10:45:33 +0000 |
commit | b6457e0f95c2563f745bbfb64c739929bc0dc901 (patch) | |
tree | 5dbd15c4261828b691ae097088f41450a8319573 /swiftclient/client.py | |
parent | 925c01ebfbdfb6478a3786f24a9572deae40f8f8 (diff) | |
download | python-swiftclient-b6457e0f95c2563f745bbfb64c739929bc0dc901.tar.gz |
Allow reading from object body on download
Currently, get_object returns a generator. This allows access to the
object's data in multiples of 'resp_chunk_size'.
This patch adds a read function to also allow accessing the object data
using read(size).
This allows, for example, the consumer of an object (where no byte range
has been specified) to read up to certain boundaries while streaming to
a new Large Object with segments of a specified size.
Reading and chunking can be safely mixed.
Related-Bug: 1367925
Co-Authored-By: Clay Gerrard <clay.gerrard@gmail.com>
Change-Id: I1cfb67f45afc7015fd896f1a89bebae048871769
Diffstat (limited to 'swiftclient/client.py')
-rw-r--r-- | swiftclient/client.py | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/swiftclient/client.py b/swiftclient/client.py index 0c6d8d3..87b3e5a 100644 --- a/swiftclient/client.py +++ b/swiftclient/client.py @@ -138,6 +138,37 @@ def encode_meta_headers(headers): return ret +class _ObjectBody(object): + """ + Readable and iterable object body response wrapper. + """ + + def __init__(self, resp, chunk_size): + """ + Wrap the underlying response + + :param resp: the response to wrap + :param chunk_size: number of bytes to return each iteration/next call + """ + self.resp = resp + self.chunk_size = chunk_size + + def read(self, length=None): + return self.resp.read(length) + + def __iter__(self): + return self + + def next(self): + buf = self.resp.read(self.chunk_size) + if not buf: + raise StopIteration() + return buf + + def __next__(self): + return self.next() + + class HTTPConnection(object): def __init__(self, url, proxy=None, cacert=None, insecure=False, ssl_compression=False, default_user_agent=None): @@ -874,13 +905,7 @@ def get_object(url, token, container, name, http_conn=None, http_reason=resp.reason, http_response_content=body) if resp_chunk_size: - - def _object_body(): - buf = resp.read(resp_chunk_size) - while buf: - yield buf - buf = resp.read(resp_chunk_size) - object_body = _object_body() + object_body = _ObjectBody(resp, resp_chunk_size) else: object_body = resp.read() http_log(('%s%s' % (url.replace(parsed.path, ''), path), method,), |