Created
May 10, 2022 00:38
-
-
Save ckandoth/84502aa2f03026b465d7ac68f8e67a29 to your computer and use it in GitHub Desktop.
Test upload to Azure blob using Python SDK and MSAL tokens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Prereqs: Run "az login" to get a refresh token at "~/.azure/msal_token_cache.json" which expires only if unused for 90 days | |
# Depends: pip install azure-identity azure-storage-blob | |
# Sources: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/storage/azure-storage-blob/samples/blob_samples_containers.py | |
STORAGE_ACCOUNT_URL = "https://blahdiblahdiblah.blob.core.windows.net" | |
CONTAINER_NAME = "mdlhot" | |
# Use the MSAL refresh token to get a temporary access token for use with blob storage libraries | |
from azure.identity import AzureCliCredential | |
az_cli_credential = AzureCliCredential() | |
from azure.storage.blob import BlobServiceClient | |
blob_service_client = BlobServiceClient(account_url=STORAGE_ACCOUNT_URL, credential=az_cli_credential) | |
container_client = blob_service_client.get_container_client(CONTAINER_NAME) | |
# Upload a test file to the blob storage container | |
with open("/var/log/lastlog", "rb") as data: | |
blob_client = container_client.upload_blob(name="lastlog", data=data) | |
# List blobs in the storage container | |
blobs_list = container_client.list_blobs() | |
for blob in blobs_list: | |
print(blob.name) | |
# Delete the blob by referring to it by name | |
container_client.delete_blob("lastlog") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment