From 8677fc515345c2de07749abfcc44349d02f97960 Mon Sep 17 00:00:00 2001 From: AlexisPPLIN Date: Thu, 22 Aug 2024 11:12:04 +0200 Subject: [PATCH 1/2] Added x-amz-checksum-sha256 header for Object Lock support --- S3/Crypto.py | 8 ++++++++ S3/S3.py | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/S3/Crypto.py b/S3/Crypto.py index 72302ed7..136443e4 100644 --- a/S3/Crypto.py +++ b/S3/Crypto.py @@ -346,3 +346,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 = base64.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 From 71b8ab39900fab662295306220e562ecdb67c162 Mon Sep 17 00:00:00 2001 From: AlexisPPLIN Date: Thu, 12 Sep 2024 15:11:13 +0200 Subject: [PATCH 2/2] Fixed import of b64encode from base64 --- S3/Crypto.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/S3/Crypto.py b/S3/Crypto.py index 136443e4..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 @@ -351,6 +353,6 @@ 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 = base64.b64encode(sha256_hash_digest).decode() + sha256_hash_digest_b64 = b64encode(sha256_hash_digest).decode() return sha256_hash_digest_b64 __all__.append("sha256_hash_to_base64")