How to download an Azure Blob Storage file via URL in Python?

Viewed 11491

I am trying to download an Azure Blob Storage file from my storage account, to do so, I have checked what the URL is and I am doing the following:

with urllib.request.urlopen("<url_file>") as resp:
    img = np.asarray(bytearray(resp.read()), dtype="uint8")

But I am getting the following error:

urllib.error.HTTPError: HTTP Error 404: The specified resource does not exist.

I have doubled checked that the url is correct. Could this have something to do with not having passed the keys of my subscription or any other info about the Storage Account?

Any idea?

4 Answers

As on Dec 26, 2019 I am unable to import BaseBlobService from azure cloud storage. Neither of BlobPermissions, generate_blob_shared_access_signature worked for me. Below is something I used and it worked in my case and hope it helps

from azure.storage.blob import generate_blob_sas, AccountSasPermissions

def scan_product():

    account_name=<account_name>
    container_name=<container_name>
    blob_name=<blob_name>
    account_key=<account_key>
    url = f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}"
    sas_token = generate_blob_sas(
        account_name=account_name,
        account_key=account_key,
        container_name=container_name,
        blob_name=blob_name,
        permission=AccountSasPermissions(read=True),
        expiry=datetime.utcnow() + timedelta(hours=1)
    )

    url_with_sas = f"{url}?{sas_token}"

Actually, you can generate a blob url with sas token in Azure Storage SDK for Python for accessing directly, as my sample code below.

from azure.storage.blob.baseblobservice import BaseBlobService
from azure.storage.blob import BlobPermissions
from datetime import datetime, timedelta

account_name = '<account name>'
account_key = '<account key>'
container_name = '<container name>'
blob_name = '<blob name>'

url = f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}"

service = BaseBlobService(account_name=account_name, account_key=account_key)
token = service.generate_blob_shared_access_signature(container_name, blob_name, permission=BlobPermissions.READ, expiry=datetime.utcnow() + timedelta(hours=1),)

url_with_sas = f"{url}?{token}"

Then,

import urllib
import numpy as np

req = urllib.urlopen(url_with_sas)
img = np.asarray(bytearray(req.read()), dtype=np.uint8)

To solve the issue all I needed to do was to change the Blob Storage access level to Blob (anonymous read access for blob only). Once this is done, it will work.

enter image description here

Related