我想知道如何删除Azure存储队列上的消息。目前我不断收到以下错误。
<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:47de86b5-3003-004d-0fc0-dd56e6000000
Time:2024-07-24T11:56:26.1466778Z</Message><AuthenticationErrorDetail>The MAC signature found in the HTTP request '<signature>' is not the same as any computed signature. Server used following string to sign: 'DELETE
x-ms-date:Wed, 24 Jul 2024 11:56:25 GMT
x-ms-version:2016-05-31
/gildedhonordev/ghmatchedtickets/messages/<messageid>'.</AuthenticationErrorDetail></Error>
使用此页面作为指导,我创建了一个成功获取排队消息的函数。然而,当我想删除一条消息时,同样的方法在授权时总是失败。
以下是我的代码:
get_queued_message() {
DATE_ISO=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")
VERSION="2016-05-31"
HEADER_RESOURCE="x-ms-date:$DATE_ISO\nx-ms-version:$VERSION"
URL_RESOURCE="/$STORAGE_ACC/$QUEUE_NAME/messages"
STRING_TO_SIGN="GET\n\n\n\n\n\n\n\n\n\n\n\n$HEADER_RESOURCE\n$URL_RESOURCE"
DECODED_KEY="$(echo -n "$STORAGE_KEY" | base64 -d -w0 | xxd -p -c256)"
SIGN=$(printf "$STRING_TO_SIGN" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$DECODED_KEY" -binary | base64 -w0)
RET=$(curl -X GET \
-H "x-ms-date:$DATE_ISO" \
-H "x-ms-version:$VERSION" \
-H "Authorization: SharedKey $STORAGE_ACC:$SIGN" \
-H "Content-Length:0" \
"https://$STORAGE_ACC.queue.core.windows.net/$QUEUE_NAME/messages")
MESSAGE=$(echo "$RET" | grep -oP '<MessageText>\K[^<]+')
MESSAGE_ID=$(echo "$RET" | grep -oP '<MessageId>\K[^<]+')
POP_RECEIPT=$(echo "$RET" | grep -oP '<PopReceipt>\K[^<]+')
}
delete_queued_message() {
DATE_ISO=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")
VERSION="2016-05-31"
HEADER_RESOURCE="x-ms-date:$DATE_ISO\nx-ms-version:$VERSION"
URL_RESOURCE="/$STORAGE_ACC/$QUEUE_NAME/messages/$MESSAGE_ID?popreceipt=$POP_RECEIPT"
STRING_TO_SIGN="DELETE\n\n\n\n\n\n\n\n\n\n\n\n$HEADER_RESOURCE\n$URL_RESOURCE"
DECODED_KEY="$(echo -n "$STORAGE_KEY" | base64 -d -w0 | xxd -p -c256)"
SIGN=$(printf "$STRING_TO_SIGN" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$DECODED_KEY" -binary | base64 -w0)
curl -X DELETE \
-H "x-ms-date:$DATE_ISO" \
-H "x-ms-version:$VERSION" \
-H "Authorization: SharedKey $STORAGE_ACC:$SIGN" \
"https://$STORAGE_ACC.queue.core.windows.net/$QUEUE_NAME/messages/$MESSAGE_ID?popreceipt=$POP_RECEIPT"
}
如有任何帮助,我们将不胜感激
如何在 bash 中使用 REST API 删除 Azure 存储队列上的消息?
您可以使用以下脚本来使用 Bash
Delete Azure Storage Queue message
。
脚本:
#!/bin/bash
# Function to convert base64 to hex
base64_to_hex() {
echo -n "$1" | base64 --decode | od -An -tx1 | tr -d ' \n'
}
# Function to delete the queued message
delete_queued_message() {
STORAGE_ACC=$1
STORAGE_KEY=$2
QUEUE_NAME=$3
MESSAGE_ID=$4
POP_RECEIPT=$5
DATE_ISO=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")
VERSION="2020-04-08"
HEADER_RESOURCE="x-ms-date:$DATE_ISO\nx-ms-version:$VERSION"
CANONICALIZED_RESOURCE="/$STORAGE_ACC/$QUEUE_NAME/messages/$MESSAGE_ID\npopreceipt:$POP_RECEIPT"
STRING_TO_SIGN="DELETE\n\n\n\n\n\n\n\n\n\n\n\n$HEADER_RESOURCE\n$CANONICALIZED_RESOURCE"
DECODED_KEY=$(base64_to_hex "$STORAGE_KEY")
SIGN=$(printf "$STRING_TO_SIGN" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$DECODED_KEY" -binary | base64)
curl -X DELETE \
-H "x-ms-date:$DATE_ISO" \
-H "x-ms-version:$VERSION" \
-H "Authorization: SharedKey $STORAGE_ACC:$SIGN" \
"https://$STORAGE_ACC.queue.core.windows.net/$QUEUE_NAME/messages/$MESSAGE_ID?popreceipt=$POP_RECEIPT"
}
STORAGE_ACC="venkat326123"
STORAGE_KEY="T3czZpu1gZzzzzzzztD9nyWw=="
QUEUE_NAME="queue1"
MESSAGE_ID="aeacazzzzzf6914a"
POP_RECEIPT="AgAzzzAAAA09p8nc3d2gE="
delete_queued_message $STORAGE_ACC $STORAGE_KEY $QUEUE_NAME $MESSAGE_ID $POP_RECEIPT
上面的脚本在我的环境中执行并删除了 azure 队列消息。
传送门: