Skip to content

Add 'readinto' shim to botocore.response.StreamingBody #3108

Closed
@jimdigriz

Description

@jimdigriz

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestThis issue requests a feature.p2This is a standard priority issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions