azurerestblobazure-blob-storagebinaryfiles

How to upload images using postman to azure blob storage


I have been trying to upload a local image to my blob container folder using Postman.

Here is the link I am using to get the Javascript code to generate the signature.

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; 

I have used this resource https://learn.microsoft.com/en-us/rest/api/storageservices/put-block as a reference to generate API requests. I have turned on Cors also.

Kindly share the solution as to how would I upload a jpg or png image to my blob using REST api?


Solution

  • If we want to upload an image to the azure storage, please have a try to use the Put blob API not Put block API.

    And have a try to use the following 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"   
    

    I test it on my side, it works correctly on site.

    Headers :

    enter image description here

    Body:

    enter image description here

    Note: we could get the Content-Length from the file size.

    enter image description here