Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload images using postman to azure blob storage

I have been trying to upload an image to my blob container folder using postman Here is the link Authorization of Azure Storage service REST API am using to generate signature and am attaching filename file field in the body while body.

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 these https://learn.microsoft.com/en-us/rest/api/storageservices/put-block ,https://learn.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-services references for above code.

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 postman.

Thanks in advance

like image 702
Manish Kongari Avatar asked Dec 23 '22 13:12

Manish Kongari


2 Answers

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

like image 112
Tom Sun - MSFT Avatar answered Jan 01 '23 10:01

Tom Sun - MSFT


Not really an answer to your question, but I see a number of issues that could cause this problem you're facing. Some of the issues I noticed are:

  1. Request URL does not include the name of the file you're uploading. Your request URL should be https://colony7.blob.core.windows.net/folder-customer-profilepic/Home - explorar.jpg.
  2. Content type request header is sent as image/jpg. However, in your stringToSign it is set as image/jpeg; charset=UTF-8. Both of them should exactly match.
  3. Content length header is missing in stringToSign.
  4. Based on the documentation here, your stringToSign corresponds to SharedKeyLite however when creating the authorization header, you are using SharedKey.
  5. Your CanonicalizedHeaders does not include x-ms-version.
  6. If you intend to use SharedKey, then your stringToSign should be constructed differently. Please see the documentation link you shared for more details.

Please fix these errors and update your question with the latest screenshots/values.

like image 40
Gaurav Mantri Avatar answered Jan 01 '23 09:01

Gaurav Mantri