首页 文章

如何使用邮差将图像上传到azure blob存储

提问于
浏览
0

我一直在尝试使用邮递员将图像上传到我的blob容器文件夹,下面是截图
postman screenshot

这是链接Authorization of Azure Storage service REST API用于生成签名并在正文中附加文件名文件字段 .

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://docs.microsoft.com/en-us/rest/api/storageservices/put-blockhttps://docs.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-services引用 .

我也打开了角色 . 请分享关于如何使用邮递员将jpg或png图像上传到我的blob的解决方案 .

提前致谢

2 回答

  • 2

    如果我们要将图像上传到azure存储,请尝试使用Put blob API not 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

  • 3

    对你的问题不是一个真正的答案,但我看到一些可能导致你面临这个问题的问题 . 我注意到的一些问题是:

    • 请求网址不包含您要上传的文件的名称 . 您的请求网址应为 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 应该以不同的方式构建 . 有关更多详细信息,请参阅您共享的文档链接 .

    请修复这些错误并使用最新的屏幕截图/值更新您的问题 .

相关问题