generated from UMassCDS/PythonProjectTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Step 1: download azure cli\n", | ||
"\n", | ||
"https://learn.microsoft.com/en-us/cli/azure/install-azure-cli\n", | ||
"\n", | ||
"# Step 2: Log in with az login \n", | ||
"\n", | ||
"https://learn.microsoft.com/en-us/cli/azure/authenticate-azure-cli" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Step 3: Create python virtual env we have 3.10\n", | ||
"Packages:\n", | ||
"\n", | ||
"azure-common==1.1.28\n", | ||
"\n", | ||
"azure-core==1.26.1\n", | ||
"\n", | ||
"azure-identity==1.12.0\n", | ||
"\n", | ||
"azure-keyvault==4.2.0\n", | ||
"\n", | ||
"azure-keyvault-secrets==4.6.0\n", | ||
"\n", | ||
"azure-storage-blob==12.14.1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install azure-common==1.1.28\n", | ||
"\n", | ||
"!pip install azure-core==1.26.1\n", | ||
"\n", | ||
"!pip install azure-identity==1.12.0\n", | ||
"\n", | ||
"!pip install azure-keyvault==4.2.0\n", | ||
"\n", | ||
"!pip install azure-keyvault-secrets==4.6.0\n", | ||
"\n", | ||
"!pip install azure-storage-blob==12.14.1" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-06-06T14:37:38.121268100Z", | ||
"start_time": "2024-06-06T14:37:35.422323400Z" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from azure.keyvault.secrets import SecretClient\n", | ||
"from azure.storage.blob import BlobClient, BlobServiceClient\n", | ||
"from azure.identity import DefaultAzureCredential, AzureCliCredential\n", | ||
"import sys\n", | ||
"\n", | ||
"\n", | ||
"keyvault_url = 'https://<keyvault name virginia>.vault.azure.net/'\n", | ||
"secret_name = '<value given by virginia>'\n", | ||
"\n", | ||
"def get_az_credential():\n", | ||
" \"\"\"Obtains an Azure credential, based on the environment you run from (Linux, Windows (local vs DSVM))\n", | ||
" \n", | ||
" Parameters:\n", | ||
" None\n", | ||
" \n", | ||
" Returns:\n", | ||
" - Credential: Credential for connecting to Azure\n", | ||
" \"\"\"\n", | ||
" credential = AzureCliCredential()\n", | ||
" \n", | ||
" return credential\n", | ||
"\n", | ||
"# Connect to keyvault and fetch secret\n", | ||
"def get_keyvault_secret(keyvault_url,secret_name, credential):\n", | ||
" \"\"\"Get a secret from the keyvault\n", | ||
" \n", | ||
" Parameters:\n", | ||
" - keyvault_url (string): The URL of the storage account.\n", | ||
" - secret_name (string): The name of the container in the storage account.\n", | ||
" - credential (object): The credentials to be used for authentication.\n", | ||
" \n", | ||
" Returns:\n", | ||
" - String containing the secret\n", | ||
" \"\"\"\n", | ||
" client = SecretClient(vault_url=keyvault_url, credential=credential)\n", | ||
" return client.get_secret(secret_name)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 31, | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-06-06T14:06:26.863901Z", | ||
"start_time": "2024-06-06T14:06:25.605394700Z" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"credential = get_az_credential()\n", | ||
"storage_account_key = get_keyvault_secret(keyvault_url, secret_name , credential)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from azure.storage.blob import BlobServiceClient\n", | ||
"def list_blobs_in_container(storage_account_name, storage_account_key, container_name):\n", | ||
" try:\n", | ||
" # Construct the connection string\n", | ||
" connection_string = f\"DefaultEndpointsProtocol=https;AccountName={storage_account_name};AccountKey={storage_account_key.value};EndpointSuffix=core.windows.net\"\n", | ||
" \n", | ||
" # Create the BlobServiceClient object\n", | ||
" blob_service_client = BlobServiceClient.from_connection_string(connection_string)\n", | ||
"\n", | ||
" # Get the container client\n", | ||
" container_client = blob_service_client.get_container_client(container_name)\n", | ||
"\n", | ||
" # List blobs in the container\n", | ||
" blob_list = container_client.list_blobs()\n", | ||
" \n", | ||
" print(f\"Blobs in container '{container_name}':\")\n", | ||
" for blob in blob_list:\n", | ||
" print(f\"\\t{blob.name}\")\n", | ||
"\n", | ||
" except Exception as ex:\n", | ||
" print(f\"Exception: {ex}\")\n", | ||
"\n", | ||
"# Example usage\n", | ||
"storage_account_name = \"<account name from slack>\" \n", | ||
"container_name = \"<container name from slack>\"\n", | ||
"\n", | ||
"list_blobs_in_container(storage_account_name, storage_account_key, container_name)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "07354bbab23bd1645e2425723804091b7787c8b9f736ba6e074695e88a25cd31" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3.10.10 ('fenv': venv)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.10" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
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