import os
import io
import pandas as pd
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.storage.blob import ContainerClient
In [1]:
In [2]:
# Load all environment variables
load_dotenv()= os.getenv('AZ_STORAGE_EP')
account_url = os.getenv('AZ_STORAGE_CONTAINER') container_name
In [3]:
# Authenticate
= DefaultAzureCredential() default_credential
For the first time, you might need to authenticate via the Azure CLI
Download it from https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli
Install then run az login
in your terminal. Once you have logged in with your browser try the DefaultAzureCredential()
again!
In [4]:
# Connect to container
= ContainerClient(account_url, container_name, default_credential) container_client
In [5]:
# List files in container - should be empty
= container_client.list_blob_names()
blob_list for blob in blob_list:
if blob.startswith('newdir'):
print(blob)
newdir/cats.parquet
newdir/ronald.jpeg
In [6]:
# Upload file to container
with open(file='data/cats.csv', mode="rb") as data:
= container_client.upload_blob(name='newdir/cats.csv',
blob_client =data,
data=True) overwrite
In [7]:
# # Check files have uploaded - List files in container again
= container_client.list_blobs()
blob_list for blob in blob_list:
if blob['name'].startswith('newdir'):
print(blob['name'])
newdir/cats.csv
newdir/cats.parquet
newdir/ronald.jpeg
In [8]:
# Download file from Azure container to temporary filepath
# Connect to blob
= container_client.get_blob_client('newdir/cats.csv')
blob_client
# Write to local file from blob
= os.path.join('temp_data', 'cats.csv')
temp_filepath with open(file=temp_filepath, mode="wb") as sample_blob:
= blob_client.download_blob()
download_stream
sample_blob.write(download_stream.readall())= pd.read_csv(temp_filepath)
cat_data cat_data.head()
Name | Physical_characteristics | Behaviour | |
---|---|---|---|
0 | Ronald | White and ginger | Lazy and greedy but undoubtedly cutest and best |
1 | Kaspie | Small calico | Sweet and very shy but adventurous |
2 | Hennimore | Pale orange | Unhinged and always in a state of panic |
3 | Thug cat | Black and white - very large | Local bully |
4 | Son of Stripey | Grey tabby | Very vocal |
In [9]:
# Load directly from Azure - no local copy
= blob_client.download_blob()
download_stream = io.BytesIO(download_stream.readall())
stream_object = pd.read_csv(stream_object)
cat_data cat_data
Name | Physical_characteristics | Behaviour | |
---|---|---|---|
0 | Ronald | White and ginger | Lazy and greedy but undoubtedly cutest and best |
1 | Kaspie | Small calico | Sweet and very shy but adventurous |
2 | Hennimore | Pale orange | Unhinged and always in a state of panic |
3 | Thug cat | Black and white - very large | Local bully |
4 | Son of Stripey | Grey tabby | Very vocal |
In [10]:
# !!!!!!!!! Delete from Azure container !!!!!!!!!
= container_client.get_blob_client('newdir/cats.csv')
blob_client blob_client.delete_blob()
In [11]:
= container_client.list_blobs()
blob_list for blob in blob_list:
if blob['name'].startswith('newdir'):
print(blob['name'])
newdir/cats.parquet
newdir/ronald.jpeg