<jupyter_start><jupyter_text>Azure Blob Service FunctionsMSTICpy versions > 0.8.9 DescriptionThis Notebook provides an example of using the Azure Blob Storage features of MSTICpy in order interact with an Azure Blob Storage account.You must have msticpy installed to run this notebook:```%pip install --upgrade msticpy[azure]``` Installation and imports<jupyter_code>from msticpy.data.azure_blob_storage import AzureBlobStorage
from msticpy.data import data_obfus as mask<jupyter_output><empty_output><jupyter_text>The first step is to initalize the AzureBlobStorage client. Each client is specific to a seperate Azure Blob Storage account and you must pass it the account name when initalizing the client.<jupyter_code>abs = AzureBlobStorage(abs_name="abscontainer")
abs.connect()<jupyter_output><empty_output><jupyter_text>The first step when working with Azure Blob Storage is identifying the container to store blobs in, we can enumerate all the containers in the account with `.containers()`. Note that only containers you have permissons to access will be shown, the Azure Blob Storage features of MSTICpy use the same authentication methods as other Azure service elements and you can specific `auth_methods` when calling `.connect` to select the required authentication method.<jupyter_code>containers = abs.containers()
display(containers.mp_obf.obfuscate(column_map={"etag": "str", "name":"str"}))<jupyter_output>Attempting to sign-in with environment variable credentials...
obfuscating columns:
name, etag, 
done<jupyter_text>We can also create new containers with `.create_container()`<jupyter_code>abs.create_container(container_name="documenationcontainer")<jupyter_output><empty_output><jupyter_text>Once we have a container we can write blobs to the container. Blob objects can be of any type but here we are passing a simple string.<jupyter_code>abs.upload_to_blob(blob="Here is some test data", container_name="documenationcontainer", blob_name="test-blob")<jupyter_output>Upload complete<jupyter_text>We can list blobs within a container in the same way we listed containers, here you can see the blob we just created.<jupyter_code>abs.blobs(container_name="documenationcontainer")<jupyter_output><empty_output><jupyter_text>We can also return the contents of a blob like so:<jupyter_code>abs.get_blob(container_name="documenationcontainer", blob_name="test-blob")<jupyter_output><empty_output><jupyter_text>When sharing blobs its often necessary to access a SAS token that allow a user to access a specific blob for a specific amount of time, without having to have explicity permissions on the Azure Blob Storage account.In the example below we obsfucate the retruned SAS token but it will take the form of a full URL to the blob, with the required SAS token appeneded. By default tokens generated allow access for 7 days but this can be adjusted.<jupyter_code>sast = abs.get_sas_token(container_name="documenationcontainer", blob_name="test-blob")
mask.hash_string(sast)<jupyter_output><empty_output><jupyter_text>Finally we can delete a blob, below we delete the blob we just created and then list all blobs in the container, which returns nothing as it is now empty:<jupyter_code>abs.delete_blob(container_name="documenationcontainer", blob_name="test-blob")
abs.blobs(container_name="documenationcontainer")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06928337793636634, 'fraction_numerical': 0.0011894142134998512, 'mean_word_length': 7.3681592039801, 'pattern_counts': {'":': 2, '<': 35, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '305642', 'license': 'MIT', 'n_tokens_mistral': 965, 'n_tokens_neox': 944, 'n_words': 356, 'path': 'docs/notebooks/AzureBlobStorage.ipynb', 'repo_name': 'grantv9/msticpy', 'size': '3363'}