我尝试使用 boto3 将 XML 文件上传到 S3。根据亚马逊的建议,我想发送数据的 Base64 编码 MD5-128 位摘要(Content-MD5)。
https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Object.put
My Code:
with open(file, 'rb') as tempfile:
body = tempfile.read()
tempfile.close()
hash_object = hashlib.md5(body)
base64_md5 = base64.encodebytes(hash_object.digest())
response = s3.Object(self.bucket, self.key + file).put(
Body=body.decode(self.encoding),
ACL='private',
Metadata=metadata,
ContentType=self.content_type,
ContentEncoding=self.encoding,
ContentMD5=str(base64_md5)
)
当我尝试这个时, str(base64_md5) 创建一个像 'b'ZpL06Osuws3qFQJ8ktdBOw==\n'' 的字符串
在这种情况下,我收到此错误消息:
An error occurred (InvalidDigest) when calling the PutObject operation: The Content-MD5 you specified was invalid.
出于测试目的,我仅复制了前面没有“b”的值:“ZpL06Osuws3qFQJ8ktdBOw==\n”
然后我收到此错误消息:
botocore.exceptions.HTTPClientError: An HTTP Client raised and unhandled exception: Invalid header value b'hvUe19qHj7rMbwOWVPEv6Q==\n'
谁能帮我如何将上传文件保存到 S3?
Thanks,
Oliver