Azure Blob Storage is one of the services of Azure Storage, which is a storage service that can store a large amount of data such as binaries.
Blobs represent a single file or data, which is organized by a container. Each container is tied to one storage account. Therefore, the hierarchical structure is as shown in the figure below.
https://docs.microsoft.com/ja-jp/azure/storage/storage-python-how-to-use-blob-storage
Azure Storage provides SDKs for Nodejs, Python, etc. as well as .net.
This time I will try using blob storage using Python sdk.
Create an Azure Storage account from the management portal.
Once you have created a Storage account, you will get an access key. From the Storage account management screen, press [Access Key] to get the access key.
This time, you need two items, [Storage Account Name] and [Access Key].
The python used this time is 3.5.
First, install the Azure Storage python SDK from pip
pip install azure-storage
First, create a container that acts like a folder that holds blobs together.
from azure.storage.blob import BlockBlobService
from azure.storage.blob import PublicAccess
account_name='{your account name}'
account_key='{your account key}'
container_name='testcontainer'
service = BlockBlobService(account_name=account_name,account_key=account_key)
service.create_container(container_name)
When you do this, you can see that the blob in the admin screen has created the container.
Let's actually create a blob in the container and upload the data.
from azure.storage.blob import BlockBlobService
account_name='{your account name}'
account_key='{your account key}'
container_name='testcontainer'
file_path='sample1.png'
service = BlockBlobService(account_name=account_name,account_key=account_key)
service.create_blob_from_path(container_name,'sample_blob1',file_path)
You can see that the blob has been created.
By default, it's private access, so let's change all the containers to public access.
From the container screen, press Access Policy and select Container.
Click on the blob you created and try accessing the URL from your browser.
I was able to access the uploaded file from my browser.
You can get the blob list in the container with the following program.
from azure.storage.blob import BlockBlobService
account_name='{your account name}'
account_key='{your account key}'
container_name='testcontainer'
service = BlockBlobService(account_name=account_name,account_key=account_key)
blobs = service.list_blobs(container_name)
for blob in blobs:
print(blob.name)
To download the blob programmatically, do the following:
from azure.storage.blob import BlockBlobService
account_name='{your account name}'
account_key='{your account key}'
container_name='testcontainer'
blob_name='sample_blob1'
service = BlockBlobService(account_name=account_name, account_key=account_key)
service.get_blob_to_path(container_name,blob_name,'download.png')
Confirm that you have obtained the blob uploaded with the file name [download.png].
To remove a blob, do the following:
from azure.storage.blob import BlockBlobService
account_name='{your account name}'
account_key='{your account key}'
container_name='testcontainer'
blob_name='sample_blob1'
service = BlockBlobService(account_name=account_name, account_key=account_key)
service.delete_blob(container_name, blob_name)
You can see that the blob has disappeared by pressing the refresh button.
The sample code is in this repository. https://github.com/garicchi/azure-blob-python-sample
Recommended Posts