1
votes

I have uploaded some images to azure blob container for my azure ocr api to read the image and send back the output.

I have retrieved list of blobs from azure container using

 blob_service.list_blobs().

Each of these retrieved blobs are now given to shared access method

generate_blob_shared_access_signature(container_name='ocr-images',blob_name=blob.name,permission=PublicAccess.OFF,expiry='se=2015-04-30T02%3A23%3A26Z',start='st=2015-04-29T22%3A18%3A26Z')

output of shared access method is a SAS token which is then given to

blob_service.make_blob_url(container_name='ocr-images',blob_name=blob.name, sas_token=sas)

for generating url for each image

The URL generated when passed to Azure ocr api, shows error

requests.exceptions.HTTPError: 400 Client Error: Bad Request for url:https://westeurope.api.cognitive.microsoft.com/vision/v2.0/ocr?language=unk&detectOrientation=false

But when url is generated manually from azure portal, it works perfectly. Can some one help me with this issue?

2

2 Answers

1
votes

You're getting this error because you're using the method generate_blob_shared_access_signature incorrectly.

There are a number of issues with the values you're passing to this method.

For permission, you need to provide one of the possible values of BlobPermissions. Assuming you would want to read the blob contents, I would recommend that you use BlobPermissions.READ permission.

Both of your start and expiry dates are in the past. Furthermore, you just need to specify the date value and not include st= and se=.

Please try with the following code:

generate_blob_shared_access_signature(container_name='ocr-images',blob_name=blob.name,permission=BlobPermissions.READ,expiry='2019-04-09',start='2019-04-08')
0
votes

In case of 404 error while creating url from SAS token blob it is required to create a SAS token from BlobSharedAccesssignature.

Here is my code:

from azure.storage.blob import BlockBlobService
from azure.storage.blob.models import BlobPermissions
from azure.storage.blob.sharedaccesssignature import BlobSharedAccessSignature

account_name = data_dict['blob_storage_account_name']
account_key = data_dict['blob_storage_account_key']
top_level_container_name = data_dict['blob_container_name']
blob_service = BlockBlobService(account_name, account_key)
blob_shared = BlobSharedAccessSignature(account_name, account_key)

Once you have object from BlobSharedAccessSignature, call generate_blob method which create sas token for individual blob from your container

sas = blob_shared.generate_blob(container_name=top_level_container_name, blob_name=blob.name,
                                     permission=BlobPermissions.READ, expiry='2019-04-10',
                                     start='2019-04-09')   

sas_url= 'https://'+account_name+'.blob.core.windows.net'+'/'+top_level_container_name+'/'+blob.name+'?'+sas