Skip to content Skip to sidebar Skip to footer

How Do I Authenticate A User Against An Azure Storage Blob In Python?

I'm looking for a way to authenticate a user against an Azure blob container. The sample code (yep, newbie alert) works just fine, using an access key for the storage account, but

Solution 1:

You could refer to this article to authenticate with Azure Active Directory from an application for access to blobs.

1.Register your application with an Azure AD tenant

2.Grant your registered app permissions to Azure Storage

3.Python code:

import adal
from azure.storage.blob import (
    BlockBlobService,
    ContainerPermissions,
)
from azure.storage.common import (
    TokenCredential
)

RESOURCE = "https://storage.azure.com/"
clientId = "***"
clientSecret = "***="
tenantId = "***"
authority_url = "https://login.microsoftonline.com/" + tenantId

print(authority_url)
context = adal.AuthenticationContext(authority_url)

token = context.acquire_token_with_client_credentials(
    RESOURCE,
    clientId,
    clientSecret)
print(token)

tokenCre = TokenCredential(token["accessToken"])

blobService = BlockBlobService(account_name="***", token_credential=tokenCre)

blobService.list_blobs(container_name="***")
for i in blobService.list_blobs(container_name="***"):
    print(i.properties.name)

Post a Comment for "How Do I Authenticate A User Against An Azure Storage Blob In Python?"