Skip to content

Instantly share code, notes, and snippets.

@djouallah
Last active March 16, 2025 01:17
Show Gist options
  • Save djouallah/8caab386cf3ba3198fcb8d6f404f4480 to your computer and use it in GitHub Desktop.
Save djouallah/8caab386cf3ba3198fcb8d6f404f4480 to your computer and use it in GitHub Desktop.
from datetime import datetime, timezone, timedelta
import json
import os
from azure.identity import DefaultAzureCredential
from azure.storage.blob import (
BlobClient,
BlobSasPermissions,
BlobServiceClient,
generate_blob_sas,
)
credential = DefaultAzureCredential(exclude_workload_identity_credential=True, exclude_developer_cli_credential=True)
#print(credential.get_token('https://storage.azure.com'))
accountName = "onelake"
workspaceName = "yourWorkspace"
lakehouseName = "yourLakehouse.Lakehouse/Files"
key_start_time = datetime.now(timezone.utc)
key_expiry_time = datetime.now(timezone.utc) + timedelta(hours=0.5)
print(key_start_time)
print(key_expiry_time)
url = f"https://{accountName}.blob.fabric.microsoft.com"
blob_service_client = BlobServiceClient(url, credential=credential)
udk = blob_service_client.get_user_delegation_key(
key_start_time,
key_expiry_time)
sas = generate_blob_sas(
account_name= accountName,
container_name=workspaceName,
blob_name=lakehouseName,
user_delegation_key=udk,
permission=BlobSasPermissions(read=True,write=True, execute=True, list=True),
start = key_start_time,
expiry = key_expiry_time
)
sas_url = (
f'https://{accountName}.blob.fabric.microsoft.com/'
f'{workspaceName}/{lakehouseName}?{sas}'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment