The method of uploading a file to Azure Storage is described below.
We are building in the following environment.
OS: Windows10 Language: Python 3.8.6
Have an Azure environment You have created a new account in the "Storage Account" service of Azure
① Install the Azure Storage package for Python. $ py -m pip install azure-storage-blob == 12.5.0
② In the Azure portal, select Blob service → Container from the "Storage Account" service. Then check the connection string.
③ Execute "sample_upload.py" that describes the following program. In addition, place "Hello World.txt" (contents are free) at the execution position.
$ py sample_upload.py
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
#Check the connection string on Azure Storage.
connect_str = <Connection string>
# Create a file in local data directory to upload and download
container_name = "<Container name>"
#Strictly speaking, Azure Storage is not a folder.
#For the time being"\"It is possible to realize a pseudo folder structure by separating
upload_path = "<Upload destination folder name>\"
#File name to upload(Describe the relative file path according to the execution position)
local_file_name = "HelloWorld.txt"
#Upload destination
upload_file_path = os.path.join(upload_path, local_file_name)
#Create an instance to connect to your Azure Storage.
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
#Create a client instance of blob (file) that connects to the specified container of Azure Storage.
blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)
print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)
#Upload to Azure Storage
with open(upload_file_path, "rb") as data:
blob_client.upload_blob(data)
Azure Official Document Python Reference BlobServiceClientClass https://docs.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient?view=azure-python