boto / botocore

The low-level, core functionality of boto3 and the AWS CLI.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add 'readinfo' shim to botocore.response.StreamingBody

jimdigriz opened this issue · comments

Describe the feature

Provide a readinto method for botocore.response.StreamingBody.

Use Case

Since Python 3.11 we now have hashlib.file_digest() which can take a fileobj-ish object and return the digest, this is useful to strap to the back of .get()['Body'] without having to do the slurping of the stream yourself.

This does not work as hashlib.py:file_digest() tests for readinto and finds it missing, so raises a ValueError exception.

Proposed Solution

This is an example of shimming it into place and it Works For Me(tm):

def checksum(key):

    def _readinto(self, buf):

        return self._raw_stream.readinto(buf)

    s3 = s3_resource()
    object = s3.Object(key=key)

    if object.checksum_sha256:
        return b64decode(object.checksum_sha256).hex()

    response = object.get()
    setattr(response['Body'].__class__, 'readinto', _readinto)

    checksum = hashlib.file_digest(response['Body'], 'sha256')

    return checksum.hexdigest()

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

SDK version used

1.34.29

Environment details (OS name and version, etc.)

Debian GNU/Linux 12 (bookworm)

Thanks for the feature request, I'll mark this issue for further review by the team. Please let us know if there are any further details you can share regarding your use case and any alternatives you have tried.