How to upload images using postman to azure blob storage
Asked Answered
I

4

5

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?

Inhalator answered 19/10, 2017 at 16:47 Comment(0)
H
7

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

Herthahertz answered 20/10, 2017 at 8:20 Comment(0)
N
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.)

Nogas answered 20/8, 2020 at 13:7 Comment(0)
C
3

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.

Capet answered 20/10, 2017 at 8:21 Comment(0)
A
1

Below are working examples using the Python interface (including the signature procedure) to compute UPLOAD/DOWNLOAD jobs to Azure Blob Storage using REST API with Shared Key Authorization.

Pay attention to:

  • set the correct strToSign path
  • specify the correct Content type (that should match the file format)
  • use the exact blobname and Content length

UPLOAD

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) 

DOWNLOAD

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)
Ayeshaayin answered 23/4, 2024 at 10:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.