小编Mes*_*kov的帖子

如何使用 Content-MD5 将对象放入 s3

我尝试使用 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 。放

我的代码:

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)
        )
Run Code Online (Sandbox Code Playgroud)

当我尝试这个时 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'

任何人都可以帮助我如何将上传文件保存到 …

python amazon-s3 amazon-web-services boto3

11
推荐指数
2
解决办法
7414
查看次数

标签 统计

amazon-s3 ×1

amazon-web-services ×1

boto3 ×1

python ×1