我一直在尝试使用 Postman 将图像上传到我的 blob 容器文件夹。
这里是我用来获取Javascript代码来生成签名的链接。
var key = "[Storage account key]";
var strTime = (new Date()).toUTCString();
var strToSign = 'PUT\n\nimage/jpeg; charset=UTF-8\n\nx-ms-date:' + strTime + '\nx-ms-meta-m1:v1\nx-ms-meta-m2:v2\n/colony7/folder-customer-profilepic/Home - explorar.jpg';
var secret = CryptoJS.enc.Base64.parse(key);
var hash = CryptoJS.HmacSHA256(strToSign, secret);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = "SharedKey colony7:"+hashInBase64;
我已使用此资源 https://learn.microsoft.com/en-us/rest/api/storageservices/put-block 作为生成 API 请求的参考。我也开启了Cors。
请分享关于如何将 jpg 或 png 图像上传到我的 blob 的解决方案?
方法:
PUT
。
URL方案:
(https://{{storageName}}.blob.core.windows.net/{{Container}}/{{ImageName.png}}?{{SAS Token}})
标题:
"Content-Type": "image/png",
"Content-Length": "{{size in Bytes}}",
"x-ms-blob-type": "BlockBlob"
Body:选择二值添加图像 (标题和 URL 中的图像名称应相同。)
这并不是您问题的真正答案,但我看到许多问题可能会导致您面临的这个问题。我注意到的一些问题是:
https://colony7.blob.core.windows.net/folder-customer-profilepic/Home - explorar.jpg
。image/jpg
形式发送。但是,在您的 stringToSign
中,它被设置为 image/jpeg; charset=UTF-8
。两者应该完全匹配。stringToSign
中缺少内容长度标头。stringToSign
对应于 SharedKeyLite
,但是在创建授权标头时,您使用的是 SharedKey
。CanonicalizedHeaders
不包括 x-ms-version
。SharedKey
,那么您的 stringToSign
应该以不同的方式构造。请参阅您共享的文档链接以了解更多详细信息。请修复这些错误并使用最新的屏幕截图/值更新您的问题。
下面是使用 Python 接口(包括签名过程)的工作示例,使用带有 共享密钥授权的 REST API 来计算到 Azure Blob 存储的上传/下载作业。
注意:
上传
import requests
import hashlib
import base64
import hmac
import datetime
with open('my-fancy-image.png', 'rb') as file:
data = file.read()
blobname = "my-fancy-image.png"
accountname = "my-accountname"
container = "my-container"
api_version = "2015-02-21"
content_length = str(len(data))
content_type = "image/png"
key = "my-key"
key = base64.b64decode(key)
date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
strToSign = f"PUT\n\n\n{content_length}\n\n{content_type}\n\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:{date}\nx-ms-version:{api_version}\n/{accountname}/{container}/{blobname}"
hashed = hmac.new(key, strToSign.encode('utf-8'), hashlib.sha256)
hashInBase64 = base64.b64encode(hashed.digest()).strip()
auth = f"SharedKey {accountname}:{hashInBase64.decode('utf-8')}"
url = f"https://{accountname}.blob.core.windows.net/{container}/{blobname}"
headers = {
"x-ms-version": api_version,
"x-ms-date": date,
"Content-Type": content_type,
"x-ms-blob-type": "BlockBlob",
"Authorization": auth,
"Content-Length": content_length,
}
response = requests.put(url, headers=headers, data=data)
print(response.status_code, response.reason)
下载
import requests
import hashlib
import base64
import hmac
import datetime
blobname = "my-fancy-image.png"
accountname = "my-accountname"
container = "my-container"
api_version = "2015-02-21"
key = "my-key"
key = base64.b64decode(key)
date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
strToSign = f"GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:{date}\nx-ms-version:{api_version}\n/{accountname}/{container}/{blobname}"
hashed = hmac.new(key, strToSign.encode('utf-8'), hashlib.sha256)
hashInBase64 = base64.b64encode(hashed.digest()).strip()
auth = f"SharedKey {accountname}:{hashInBase64.decode('utf-8')}"
url = f"https://{accountname}.blob.core.windows.net/{container}/{blobname}"
headers = {
"x-ms-version": api_version,
"x-ms-date": date,
"Authorization": auth,
}
response = requests.get(url, headers=headers)
print(response.status_code, response.reason)