python/boto3 sqs 客户端如何获取 MD5OfMessageBody?这是由客户端生成的还是由服务器接收的?例如:
{
"response": {
"MD5OfMessageBody": "d73e2ed0c3817b077da8f9571426fb23",
"MessageId": "69c3463d-0f60-4a1a-94d7-fb2949953b35",
"ResponseMetadata": {
"HTTPHeaders": {
"access-control-allow-headers": "authorization,cache-control,content-length,content-md5,content-type,etag,location,x-amz-acl,x-amz-content-sha256,x-amz-date,x-amz-request-id,x-amz-security-token,x-amz-tagging,x-amz-target,x-amz-user-agent,x-amz-version-id,x-amzn-requestid,x-localstack-target,amz-sdk-invocation-id,amz-sdk-request",
"access-control-allow-methods": "HEAD,GET,PUT,POST,DELETE,OPTIONS,PATCH",
"access-control-allow-origin": "*",
"access-control-expose-headers": "etag,x-amz-version-id",
"connection": "close",
"content-length": "396",
"content-type": "text/xml",
"date": "Fri, 04 Aug 2023 11:26:40 GMT",
"server": "hypercorn-h11"
},
"HTTPStatusCode": 200,
"RequestId": "2834eec8-a1e4-4be3-b51f-127fe2e50205",
"RetryAttempts": 0
}
}
}
我在 Boto3 客户端中看不到任何似乎可以执行此操作的内容,但有一些 MD5 初始化检查,听起来这是来自 SQS 的实际响应?
这是由服务创建的响应的一部分。
MD5OfMessageBody
属性,即非 URL 编码消息正文字符串的 MD5 摘要。
您可以使用此属性来验证 Amazon SQS 是否正确接收消息。 Amazon SQS 在创建 MD5 摘要之前对消息进行 URL 解码。
下面的例子演示了如何在本地计算消息的MD5,并与SQS返回的MD5进行比较:
import boto3
import hashlib
# Create message and calculate MD5
message = 'Hello World&1ü'
client_side_md5 = hashlib.md5(message.encode()).hexdigest()
print(f'MD5 calculated locally: {client_side_md5}')
# Send message to SQS and get server-side MD5 from response
sqs = boto3.resource('sqs')
queue = sqs.get_queue_by_name(QueueName='Fulfilment-Service-Queue')
response = queue.send_message(MessageBody=message)
service_side_md5 = response['MD5OfMessageBody']
print(f'MD5 calculated by SQS: {service_side_md5}')
# Check if they match
if client_side_md5 == service_side_md5:
print(f'Transmission succeeded.')
else:
print(f'Error in transmission.')