Download Blob From Blob Storage Using Python

Viewed 3240

I am trying to download an excel file on a blob. However, it keeps generating the error "The specified blob does not exist". This error happens at blob_client.download_blob() although I can get the blob_client. Any idea why or other ways I can connect using managed identity?

default_credential = DefaultAzureCredential()
 
blob_url = BlobServiceClient('url', credential = default_credential)
   
container_client = blob_url.get_container_client('xx-xx-data')

blob_client = container_client.get_blob_client('TEST.xlsx')

downloaded_blob = blob_client.download_blob()

df=pd.read_excel(downloaded_blob.content_as_bytes(), sheet_name='Test',skiprows=2)
2 Answers

Turns out that I have to also provide 'Reader' access on top of 'Storage Blob Data Contributor' to be able to identify the blob. There was no need for SAS URL.

The reason you're getting this error is because each request to Azure Blob Storage must be an authenticated request. Only exception to this is when you're reading (downloading) a blob from a public blob container. In all likelihood, the blob container holding this blob is having a Private ACL and since you're sending an unauthenticated request, you're getting this error.

I would recommend using a Shared Access Signature (SAS) URL for the blob with Read permission instead of simple blob URL. Since a SAS URL has authorization information embedded in the URL itself (sig portion), you should be able to download the blob provided SAS is valid and has not expired.

Please see this for more information on Shared Access Signature: https://docs.microsoft.com/en-us/rest/api/storageservices/delegate-access-with-shared-access-signature.

Related