Do Azure Your One Stop Hub to learn about Cloud (Microsoft Azure, AWS & Google Cloud)

Consuming Azure Storage Python SDK using PTVS

C

Hello, everyone. If you are reading this article let’s hope you are familiar with Blobs and their types. Today, let’s do a proof of concept for Azure Blob not using the .NET SDK, nor using REST but using Python SDK for Azure Storage. When you talk about Python,
Visual Studio supports Python for developing applications on either the web or console. You just need to grab PTVS installer and you are ready to go. Without wasting time, let’s 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 is rich in IntelliSense, support for versions and virtual environments of python.
https://www.visualstudio.com/vs/python/

Updating pip

By this time you should be done with your PTVS setup, but just to make things clear we are going to install the SDK of Python using pip. Pip’s version 7 does not work when we install Azure Storage SDK.

It asks you to update pip’s version to 9 so that it can fetch Azure Storage SDK. Well, let’s 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 the latest version of pip you are set for installing the Azure SDK now. Let’s 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 and the SDK is installed IntelliSenselisense is working. 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 the Python database is refreshed.

Import Azure Storage

First step you need to do after following steps mentioned above is that you need to import the SDK so that you can consume that in your Python code. Right now, import the Blob reference.
import sys from azure.storage.blob.blockblobservice import BlockBlobService from azure.storage.blob import ContentSettings

Connection

Once you have written the lines mentioned above, you need to make a connection with your Azure Storage account that’s living in the cloud (Azure). In case you don’t have it, you can subscribe for a free trial of Azure to try Azure Storage. You need to specify
Account Name and Key for storage. block_blob_service = BlockBlobService(account_name='YourAccountName', account_key='Your Key');

Different Operations

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 this repository. Here is the link:
https://github.com/muhammad92/Azure-Storage-Sample-Python-Java-

About the author

Saad Mahmood

Add comment

Do Azure Your One Stop Hub to learn about Cloud (Microsoft Azure, AWS & Google Cloud)

Saad Mahmood

Get in touch

I am 23, with over half a decade of familiarity with Software Development. My first IDE was Visual Studio and I started my journey of software development with C#. I am Microsoft's MVP for Windows Development and Xamarin's as well, which makes me contribute to Xamarin and Windows ecosystem. A man is known by its software delivery, and I believe I have done it many times. Not only having knowledge of Microsoft stack but also knowledge of JAVA, Python(dJango, Flask) makes me diverse. I believe what comes to one's mind is always possible.