Skip to content

Commit c8660d4

Browse files
geraldhansenGerald Hansen
authored andcommitted
Add ability to handle sse-c with add-header
This will suppress md5-sum checks for sse-c uploaded and downloaded files. And use extra-headers also for get and info requests to download sse-c enrypted files again.
1 parent 81be839 commit c8660d4

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

S3/S3.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,13 @@ def object_put(self, filename, uri, extra_headers = None, extra_label = ""):
706706
response = self.send_file(request, src_stream, labels)
707707
return response
708708

709-
def object_get(self, uri, stream, dest_name, start_position = 0, extra_label = ""):
709+
def object_get(self, uri, stream, dest_name, start_position = 0, extra_headers = None, extra_label = ""):
710+
headers = SortedDict(ignore_case = True)
711+
if extra_headers:
712+
headers.update(extra_headers)
710713
if uri.type != "s3":
711714
raise ValueError("Expected URI type 's3', got '%s'" % uri.type)
712-
request = self.create_request("OBJECT_GET", uri = uri)
715+
request = self.create_request("OBJECT_GET", uri = uri, headers = headers)
713716
labels = { 'source' : uri.uri(), 'destination' : dest_name, 'extra' : extra_label }
714717
response = self.recv_file(request, stream, labels, start_position)
715718
return response
@@ -924,8 +927,12 @@ def object_move(self, src_uri, dst_uri, extra_headers = None):
924927
debug("Object '%s' NOT deleted because of an unexepected response data content.", src_uri)
925928
return response_copy
926929

927-
def object_info(self, uri):
928-
request = self.create_request("OBJECT_HEAD", uri = uri)
930+
def object_info(self, uri, extra_headers = None):
931+
headers = SortedDict(ignore_case = True)
932+
if extra_headers:
933+
headers.update(extra_headers)
934+
935+
request = self.create_request("OBJECT_HEAD", uri = uri, headers=headers)
929936
response = self.send_request(request)
930937
return response
931938

@@ -1583,7 +1590,10 @@ def send_file(self, request, stream, labels, buffer = '', throttle = 0,
15831590
debug("MD5 sums: computed=%s, received=%s" % (md5_computed, response["headers"].get('etag', '').strip('"\'')))
15841591
## when using KMS encryption, MD5 etag value will not match
15851592
md5_from_s3 = response["headers"].get("etag", "").strip('"\'')
1586-
if ('-' not in md5_from_s3) and (md5_from_s3 != md5_hash.hexdigest()) and response["headers"].get("x-amz-server-side-encryption") != 'aws:kms':
1593+
if (('-' not in md5_from_s3) and
1594+
(md5_from_s3 != md5_hash.hexdigest()) and
1595+
response["headers"].get("x-amz-server-side-encryption") != 'aws:kms' and
1596+
response["headers"].get("x-amz-server-side​-encryption​-customer-key-md5") == ""):
15871597
warning("MD5 Sums don't match!")
15881598
if retries:
15891599
warning("Retrying upload of %s" % (filename))
@@ -1821,7 +1831,9 @@ def recv_file(self, request, stream, labels, start_position = 0, retries = _max_
18211831
start_position + int(response["headers"]["content-length"]), response["size"]))
18221832
debug("ReceiveFile: Computed MD5 = %s" % response.get("md5"))
18231833
# avoid ETags from multipart uploads that aren't the real md5
1824-
if ('-' not in md5_from_s3 and not response["md5match"]) and (response["headers"].get("x-amz-server-side-encryption") != 'aws:kms'):
1834+
if (('-' not in md5_from_s3 and not response["md5match"]) and
1835+
response["headers"].get("x-amz-server-side-encryption") != 'aws:kms' and
1836+
response["headers"].get("x-amz-server-side​-encryption​-customer-key-md5") == ""):
18251837
warning("MD5 signatures do not match: computed=%s, received=%s" % (
18261838
response.get("md5"), md5_from_s3))
18271839
return response

s3cmd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ def cmd_object_get(args):
548548
warning(u"Exiting now because of --dry-run")
549549
return EX_OK
550550

551+
extra_headers = copy(cfg.extra_headers)
552+
551553
seq = 0
552554
ret = EX_OK
553555
for key in remote_list:
@@ -599,7 +601,10 @@ def cmd_object_get(args):
599601
continue
600602
try:
601603
try:
602-
response = s3.object_get(uri, dst_stream, destination, start_position = start_position, extra_label = seq_label)
604+
response = s3.object_get(uri, dst_stream, destination,
605+
start_position=start_position,
606+
extra_headers=extra_headers,
607+
extra_label=seq_label)
603608
finally:
604609
dst_stream.close()
605610
except S3DownloadError as e:
@@ -916,6 +921,7 @@ def cmd_mv(args):
916921
def cmd_info(args):
917922
cfg = Config()
918923
s3 = S3(cfg)
924+
extra_headers = copy(cfg.extra_headers)
919925

920926
while (len(args)):
921927
uri_arg = args.pop(0)
@@ -925,7 +931,7 @@ def cmd_info(args):
925931

926932
try:
927933
if uri.has_object():
928-
info = s3.object_info(uri)
934+
info = s3.object_info(uri, extra_headers=extra_headers)
929935
output(u"%s (object):" % uri.uri())
930936
output(u" File size: %s" % info['headers']['content-length'])
931937
output(u" Last mod: %s" % info['headers']['last-modified'])

0 commit comments

Comments
 (0)