我尝试使用 Python 请求模块将新的 blob 上传到 Azure 存储计数器,但收到错误签名字段格式不正确。有人可以帮我理解我在这里缺少什么吗?
import requests
file_name = "test.txt"
account = "<account_name>"
container = "<container_name>"
blob = "<blob_name>"
key = "sp=rwdlacupiytfx&st=2023-07-10T15:59:34Z&se=2023-12-10T23:59:34Z&spr=https&sv=2022-11-02&sr=c&sig=<sig>"
file = open(file_name, "rb")
url = f"https://{account}.blob.core.windows.net/{container}/{blob}/{file_name}?{key}"
print(url)
headers = {'x-ms-version': '2020-10-02', 'x-ms-blob-type': 'BlockBlob'}
test_response = requests.put(url, data = file, headers = headers)
if test_response.ok:
print("Upload completed successfully!")
print(test_response.text)
else:
print("Something went wrong!")
print(test_response.text)
错误:
<?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:a0bf7a23-d01e-0073-0b7c-e59eea000000
Time:2023-09-12T13:28:03.2249045Z</Message><AuthenticationErrorDetail>Signature fields not well formed.</AuthenticationErrorDetail></Error>
我从 Azure Blob Storage Explorer 中的属性面板获取了 SAS:
我收到错误签名字段格式不正确。能 有人请帮我理解我在这里缺少什么吗?
错误 “签名字段格式不正确” 表示您在 URL 中使用的共享访问签名 (SAS) 令牌的格式存在问题。
我尝试使用以下代码并能够将 blob 上传到存储。
代码:
import requests
file_name = "path/to/file.txt"
account = "<storage_name>"
container = "<container_name>"
blob = "test3465.txt"
sas_token = "<SAS_token>" # Replace Your SAS token (excluding "?")
url = f"https://{account}.blob.core.windows.net/{container}/{blob}/{file_name}?{sas_token}"
print(url)
headers = {'x-ms-version': '2020-10-02', 'x-ms-blob-type': 'BlockBlob'}
with open(file_name, "rb") as file:
test_response = requests.put(url, data=file, headers=headers)
if test_response.ok:
print("Upload completed successfully!")
print(test_response.text)
else:
print("Something went wrong!")
print(test_response.text)
输出:
https://venkat678.blob.core.windows.net/test/test3465.txt/./test3465.txt?sv=2022-11-02&ss=bfqt&srt=co&sp=rwdlacupiytfx&se=2023-09-12T23:00:16Z&st=2023-09-12T15:00:16Z&spr=https&sig=xxxxxxxx
Upload completed successfully!
传送门: