调用 PutObject 操作时发生错误 (InvalidArgument):计算的密钥 MD5 散列与提供的散列不匹配

An error occurred (InvalidArgument) when calling the PutObject operation: The calculated MD5 hash of the key did not match the hash that was provided

如何在 python flask 中为文件 object 创建 md5 散列?我尝试使用以下代码创建:

md5_hash = hashlib.md5()
md5_hash.update(file)
digest = md5_hash.hexdigest()
base64_encoded_md5 = base64.b64encode(digest.encode("utf-8"))
base64_encoded_md5_string = str(base64_encoded_md5.decode("utf-8"))

我在 boto3 s3_client.upload_fileobj() 函数中使用 SSE-C 加密,因此也在 ExtraArgs 中传递了 SSECustomerAlgorithm、SSECustomerKey 和 SSECustomerKeyMD5。

此处,SSECustomerKeyMD5 高于 base64_encoded_md5_string 值。

我获取 MD5 哈希的实现有什么问题吗?

亚马逊如何为同一个文件创建 MD5 哈希 object?

在 boto3 依赖项中是否有任何内置函数可以用来获取 MD5 哈希值?


我调试了整个问题,boto3.s3.inject 包含 upload_fileobj() 函数。它正在上传文件并作为响应,在图像中返回下面的响应。 boto3.s3.inject response

我为这个问题苦恼了很多天。请帮忙。


[已解决]: 修改了我的加密密钥生成算法以生成 32 位字符串。早些时候是 16 位导致了这个问题。还从 upload_fileobj() 中的 ExtraArgs 中删除了 SSECustomerKeyMD5,因为 boto3 在内部计算加密密钥本身的 MD5 哈希值,并在 botocore.client._make_api_call.[=13 中发出实际请求之前将其添加到请求 headers =]

简而言之,

  1. 已从 ExtraArgs 中删除 SSECustomerKeyMD5
  2. 修改CustomerKey/EncryptionKey生成算法以生成32位字符串。

关注了 shimo 的回答和 这个回答。

你可以在 Boto3 doc 中找到这个解释,以为它不在 upload_fileobj 部分。

SSECustomerKeyMD5 (string) -- Specifies the 128-bit MD5 digest of the encryption key according to RFC 1321.

您的代码似乎在计算文件的 MD5,而不是加密密钥。