diff --git a/S3/Crypto.py b/S3/Crypto.py index 72302ed7..ef9d1661 100644 --- a/S3/Crypto.py +++ b/S3/Crypto.py @@ -20,6 +20,8 @@ # Python 2 support from base64 import encodestring +from base64 import b64encode + from . import Config from logging import debug from .BaseUtils import encode_to_s3, decode_from_s3, s3_quote, md5, unicode @@ -346,3 +348,11 @@ def calculateChecksum(buffer, mfile, offset, chunk_size, send_chunk): return md5_hash.hexdigest() __all__.append("calculateChecksum") + +def sha256_hash_to_base64(sha256_hash): + # Extract digest from sha256 hash + sha256_hash_digest = sha256_hash.digest() + # Then convert it to base64 + sha256_hash_digest_b64 = b64encode(sha256_hash_digest).decode() + return sha256_hash_digest_b64 +__all__.append("sha256_hash_to_base64") diff --git a/S3/S3.py b/S3/S3.py index d4cac8f9..1c2b42e0 100644 --- a/S3/S3.py +++ b/S3/S3.py @@ -47,7 +47,7 @@ from .ConnMan import ConnMan from .Crypto import (sign_request_v2, sign_request_v4, checksum_sha256_file, checksum_sha256_buffer, generate_content_md5, - hash_file_md5, calculateChecksum, format_param_str) + hash_file_md5, calculateChecksum, format_param_str, sha256_hash_to_base64) try: from ctypes import ArgumentError @@ -1848,6 +1848,11 @@ def send_file(self, request, stream, labels, buffer = '', throttle = 0, sha256_hash = checksum_sha256_file(stream, offset, size_total) request.body = sha256_hash + # Provide the checksum with the request. This is important for buckets that have + # Object Lock enabled. + + headers['x-amz-checksum-sha256'] = sha256_hash_to_base64(sha256_hash) + if use_expect_continue: if not size_total: use_expect_continue = False