Resource not found for a valid Azure link

I have a uploadFilesToAzure method which works and the file is getting uploaded to my Azure’s container.

But I can’t seem to get the download to work. The azure link is correct but Azure says Resource not found.

def downloadFileFromAzure(azure_url):

    # https%3A%2F%2FmyContainer.blob.core.windows.net%2Fattachments%2Fservice-orders%2F424%2Fattach-accounts%2Fdummy.txt
    print(azure_url)

    # Instantiate a BlobServiceClient using a connection string
    blob_service_client = BlobServiceClient.from_connection_string(settings.AZURE_STORAGE_ACCOUNT_STRING)

    # Name of the Azure Storage Container
    container_name = settings.AZURE_STORAGE_CONTAINER_NAME

    # Create a BlobServiceClient object
    container_client = blob_service_client.get_container_client(container_name)

    # The destination file name
    decoded_url = unquote(azure_url)
    parsed_url = urlparse(decoded_url)
    blob_name = unquote(parsed_url.path.lstrip("/"))
    blob_client = blob_service_client.get_blob_client(container_name, blob_name)

    destination_dir = os.path.join(settings.MEDIA_ROOT, 'cache')
    parsed_url = urlparse(unquote(azure_url))
    file_name = unquote(parsed_url.path.split("/")[-1])
    print(file_name)

    with open(file=os.path.join(destination_dir, file_name), mode="wb") as sample_blob:
        download_stream = blob_client.download_blob()
        sample_blob.write(download_stream.readall())

I think “Resource not found” usually occurs if there is any discrepancy in the blob’s path!

I highly recommend adding some sanity checks to see if the container/blobs exist before assuming they do and just retrieving the blob! This will help you debug/raise a more accurate Exception.

this is a Python service for azureBlob implementing those checks, I hope it helps: showme/showme/services/blob/base.py at main · wassef911/showme · GitHub