如何使用 Postman 将图像上传至 Azure Blob 存储

4
我一直在试图使用Postman将图像上传到我的Blob容器文件夹。这是我使用的链接Azure存储服务REST API的授权来生成签名,并在正文中附加了文件名文件字段。请注意,保留HTML标签。
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/zh-cn/rest/api/storageservices/put-block, https://learn.microsoft.com/zh-cn/rest/api/storageservices/authentication-for-the-azure-storage-services,以上代码的引用。

我也开启了 CORS。请告诉我如何使用 Postman 上传 JPG 或 PNG 图像到我的 Blob。

提前致谢。


这并不是直接回答你的问题,但你尝试过使用Azure Storage Explorer吗?https://azure.microsoft.com/en-ca/features/storage-explorer/ - CSharpRocks
你为什么给这个打上了S3的标签? - Mark B
3个回答

7
如果我们想要上传图片到Azure存储中,请尝试使用Put blob API而不是Put block API。同时,请使用以下strToSign。
"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:2015-12-11\n/accountname/container/blobname"   

我在我的一侧进行了测试,它在网站上正常工作。

Headers:

enter image description here

正文:

enter image description here

注意:我们可以从文件大小获取Content-Length。

enter image description here


有没有一种方法可以上传文件而不使用内容长度头,因为它受到限制? - Manish Kongari
如何通过Ajax请求将二进制PNG文件发送到Azure?当我尝试发送数据时,它只发送了0字节的数据。 - Manish Kongari
{btsdaf} - Tom Sun - MSFT
{btsdaf} - Tom Sun - MSFT

5
  1. Method: PUT.

  2. URL scheme:

    (https://{{storageName}}.blob.core.windows.net/{{Container}}/{{ImageName.png}}?{{SAS Token}})
    
  3. Headers:

    "Content-Type": "image/png",
    "Content-Length": "{{size in Bytes}}",
    "x-ms-blob-type": "BlockBlob"
    
  4. Body: select binary add image (The image name should be same in the header and the URL.)


3
并不是回答你问题的答案,但我看到了一些可能导致你面临这个问题的问题。我注意到的一些问题如下:
  1. 请求URL不包括你正在上传的文件的名称。你的请求URL应该是https://colony7.blob.core.windows.net/folder-customer-profilepic/Home - explorar.jpg
  2. 内容类型请求头发送为image/jpg。然而,在你的stringToSign中它被设置为image/jpeg; charset=UTF-8。两者应完全匹配。
  3. stringToSign中缺少内容长度头。
  4. 根据此处的文档,你的stringToSign对应于SharedKeyLite,但在创建授权头时,你正在使用SharedKey
  5. 你的CanonicalizedHeaders不包括x-ms-version
  6. 如果你打算使用SharedKey,那么你的stringToSign应该构造得不同。请参见你分享的文档链接以获取更多详细信息。
请修复这些错误,并使用最新的截图/值更新你的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接