Testing Azure Blob storage with Azurite
September 15, 2022
References:
- https://github.com/Azure-Samples/automated-testing-with-azurite
- https://azure.microsoft.com/en-us/products/storage/storage-explorer/
- https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azurite?tabs=visual-studio
- https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string#connect-to-the-emulator-account-using-the-shortcut
Script:
from azure.core.exceptions import ResourceExistsError
from azure.storage.blob import BlobServiceClient
if __name__ == "__main__":
connection_string = "AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=[http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;"](http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;")
# connection_string = "UseDevelopmentStorage=true;"
container_name = "test-container"
blob_client = BlobServiceClient.from_connection_string(connection_string)
try:
blob_client.create_container(container_name)
except ResourceExistsError:
print(f"Container '{container_name}' already exists, skipping creation.")
container_client = blob_client.get_container_client(container_name)
files = ["ONE", "TWO", "THREE"]
for count, f in enumerate(files):
blob_client = container_client.get_blob_client(f"file_{count}.txt")
blob_client.upload_blob(f, overwrite=True)
print("Done")