'How to get object from MinIO response?

I am using python API to save and download model from MinIO. This is a MinIO installed on my server. The data is in binary format.

a = 'Hello world!'
a = pickle.dumps(a)
client.put_object(
                bucket_name='my_bucket',
                object_name='my_object',
                data=io.BytesIO(a),
                length=len(a)
            )

I can see object saved through command line :

mc cat origin/my_bucket/my_object
Hello world!

However, when i try to get it through Python API :

response = client.get_object(
                bucket_name = 'my_bucket',
                object_name= 'my_object'
            )

response is a urllib3.response.HTTPResponse object here.

I am trying to read it as :

response.read()
b''

I get a blank binary string. How can I read this object? It won't be possible for me to know its length at the time of reading it.

and here is response.__dict__ :

{'headers': HTTPHeaderDict({'Accept-Ranges': 'bytes', 'Content-Length': '27', 'Content-Security-Policy': 'block-all-mixed-content', 'Content-Type': 'application/octet-stream', 'ETag': '"75687-1"', 'Last-Modified': 'Fri, 16 Jul 2021 14:47:35 GMT', 'Server': 'MinIO/DEENT.T', 'Vary': 'Origin', 'X-Amz-Request-Id': '16924CCA35CD', 'X-Xss-Protection': '1; mode=block', 'Date': 'Fri, 16 Jul 2021 14:47:36 GMT'}), 'status': 200, 'version': 11, 'reason': 'OK', 'strict': 0, 'decode_content': True, 'retries': Retry(total=5, connect=None, read=None, redirect=None, status=None), 'enforce_content_length': False, 'auto_close': True, '_decoder': None, '_body': None, '_fp': <http.client.HTTPResponse object at 01e50>, '_original_response': <http.client.HTTPResponse object at 0x7e50>, '_fp_bytes_read': 0, 'msg': None, '_request_url': None, '_pool': <urllib3.connectionpool.HTTPConnectionPool object at 0x790>, '_connection': None, 'chunked': False, 'chunk_left': None, 'length_remaining': 27}



Solution 1:[1]

Try with response.data.decode()

Solution 2:[2]

The response is a urllib3.response.HTTPResponse object.
See urllib3 Documentation:

Backwards-compatible with http.client.HTTPResponse but the response body is loaded and decoded on-demand when the data property is accessed.

Specifically, you should read the answer like this:

response.data  # len(response.data)

Or, if you want to stream the object, you have examples on the minio-py repository: examples/get_objects.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anirudh S
Solution 2 florianvazelle