Consuming Azure Storage Python SDK using PTVS
Saad Mahmood
Sr. Cloud Solution Architect - Lead @ Microsoft | HPC, Azure AI, FinOps, Resiliency, Cloud Infrastructure , Azure Open AI
Hello everyone, in last blog post of Azure Storage we discussed about Blobs and it's types. Today lets do a proof of concept for Azure Blob not using .NET SDK not using REST but using Python SDK for Azure Storage. When you talk about Python, Visual Studio supports python for developing applications, either web or console. You just need to grab PTVS installer and you are ready to go. Without wasting time lets do it.
Installing PTVS (Python Tools for Visual Studio)
If you have already installed Python tools for Visual Studio, you are good to go but in case you are not up and running with PTVS. No worries, we are going to help you out in setting up PTVS. You just need to go to this url and download Python tools for Visual Studio. Install them, install your favorite python version and you are ready to do stuff along.
PTVS are rich in intellisense, support for versions and virtual environments of python.
https://www.visualstudio.com/vs/python/
Updating pip
I hope by this time you would be done with your PTVS setup, but just to make things clear. We are going to install SDK of python using pip. Pip's version 7 does not work when we install Azure Storage SDK.
if would ask you to update pip's version to 9 so that it can fetch Azure Storage SDK. Well lets do that.
simply type --upgrade pip and hit OK. That's all you need to upgrade pip.
Installing Azure SDK via pip
As now you have installed latest verion of pip you would be good enough for installing Azure SDK now. Lets do it.
Type in azure-storage in your pip's package name text field and hit ok.
Refreshing Python Database
You would be surprised, that pip is upgraded, SDK is installed but no intellisense is working. Well that's because you need to refresh Python's database in order to get complete intellisense. But you need not to do that manually. PTVS takes care of that and you need to wait a bit so that Python database is refreshed.
Import Azure Storage
First step you need to do after following steps mentioned above is that you need to import SDK so that you can consume that in your python code. Right now I am just importing Blob reference.
import sys
from azure.storage.blob.blockblobservice import BlockBlobService
from azure.storage.blob import ContentSettings
Connection
Once you have written lines mentioned above. You need to make a connection with your Azure Storage account that's living in cloud (Azure). In case you don't have have you can subscribe for free trial of Azure and try Azure Storage. You need to specify Account Na,e and Key for storage.
block_blob_service = BlockBlobService(account_name='YourAccountName', account_key='Your Key');
Here are few of the operations that you can perform to test,
Create a Blob
def CreateBlob(name,container,type,file):
block_blob_service.create_blob_from_path(
container,
name,
file,
content_settings= ContentSettings(content_type=type)
);
List Down Blobs
def ListBlobs(container):
generator = block_blob_service.list_blobs(container)
for blob1 in generator:
print(blob1.name)
Download Blob
def DownloadBlob(container,blobname,filepath):
block_blob_service.get_blob_to_path(container,blobname,filepath)
Write Blob in Text
def WriteBlobinText(container,blobname,encodingtype):
block_blob_service.get_blob_to_text(container, blobname, encodingtype)
Delete Blob
def DeleteBlob(container,blobname):
block_blob_service.delete_blob(container, blobname)
Delete Container
def DeleteContainer(container):
block_blob_service.delete_container(container)
Create Blob From Bytes Data
def CreateBlobFromBytes(container,blobname,bytesContent):
block_blob_service.create_blob_from_bytes(container,blobname,bytesContent);
Sample on GitHub
You can clone / download repository. Here is the link
https://github.com/muhammad92/Azure-Storage-Sample-Python-Java-
Hope you enjoyed this blog post. See you guys next time.