Use Azure Blob Storage from Python

What is Azure Blob Storage?

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.

blob

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

Create an Azure Storage account from the management portal.

Screenshot from 2016-12-26 12-51-39.png

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.

Screenshot from 2016-12-26 12-53-22.png

This time, you need two items, [Storage Account Name] and [Access Key].

Library installation

The python used this time is 3.5.

First, install the Azure Storage python SDK from pip

pip install azure-storage

Make a container

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.

Screenshot from 2016-12-26 13-03-49.png

Create a blob in a 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.

Screenshot from 2016-12-26 13-06-23.png

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.

Screenshot from 2016-12-26 13-07-42.png

Click on the blob you created and try accessing the URL from your browser.

Screenshot from 2016-12-26 13-08-32.png

I was able to access the uploaded file from my browser.

Get a list of blobs

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)

get blob

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].

Remove blob

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.

Screenshot from 2016-12-26 13-15-27.png

reference

The sample code is in this repository. https://github.com/garicchi/azure-blob-python-sample

Recommended Posts

Use Azure Blob Storage from Python
Use thingsspeak from python
Use fluentd from python
Use MySQL from Python
Use MySQL from Python
Use BigQuery from python.
Use mecab-ipadic-neologd from python
Firebase: Use Cloud Firestore and Cloud Storage from Python
Use e-Stat API from Python
How to use Azure Table storage from Django (PTVS)
Use Stanford Core NLP from Python
Read and use Python files from Python
Use Azure ML Python SDK 4: Write output to Blob storage-Part 2
Forcibly use Google Translate from python
How to specify Cache-Control for blob storage in Azure Storage in Python
Using Cloud Storage from Python3 (Introduction)
Use kabu StationĀ® API from Python
Use the Flickr API from Python
Use Azure ML Python SDK 3: Write output to Blob storage-Part 1
Use fastText trained model from Python
Use Google Analytics API from Python
File upload to Azure Storage (Python)
Use PostgreSQL data type (jsonb) from Python
Use machine learning APIs A3RT from Python
I want to use jar from python
Working with Azure CosmosDB from Python Part.2
Use Google Cloud Vision API from Python
Use Django from a local Python script
Use C ++ functions from python with pybind11
Operate Sakura's cloud object storage from Python
Use Python and MeCab with Azure Functions
sql from python
MeCab from Python
Use Azure ML Python SDK 2: Use dataset as input-Part 2
Study from Python Hour7: How to use classes
[Bash] Use here-documents to get python power from bash
I want to use ceres solver from python
Use Python and word2vec (learned) with Azure Databricks
Let's use different versions of SQLite3 from Python3!
Wrap C ++ with Cython for use from Python
Use the nghttp2 Python module from Homebrew from pyenv's Python
Use Python / Django with Windows Azure Cloud Service!
[Azure Functions / Python] Chain functions with Queue Storage binding
Use Tor to connect from urllib2 [Python] [Mac]
Python: Use zipfile to unzip from standard input
Use data class for data storage of Python 3.7 or higher
Use R density ratio estimation package densratio from Python
Touch MySQL from Python 3
Use config.ini in Python
Reading CSV data from DSX object storage Python code
Operate Filemaker from Python
[Python] Use JSON with Python
Use dates in Python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Python from or import
Use Valgrind in Python
Use mecab with Python3
Run python from excel
Install python from source