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.
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.
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
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.
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.
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
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');
Here are few of the operations that you can perform to test:
def CreateBlob(name,container,type,file): block_blob_service.create_blob_from_path( container, name, file, content_settings= ContentSettings(content_type=type) );
def ListBlobs(container): generator = block_blob_service.list_blobs(container) for blob1 in generator: print(blob1.name)
def DownloadBlob(container,blobname,filepath): block_blob_service.get_blob_to_path(container,blobname,filepath)
def WriteBlobinText(container,blobname,encodingtype): block_blob_service.get_blob_to_text(container, blobname, encodingtype)
def DeleteBlob(container,blobname): block_blob_service.delete_blob(container, blobname)
def DeleteContainer(container): block_blob_service.delete_container(container)
def CreateBlobFromBytes(container,blobname,bytesContent): block_blob_service.create_blob_from_bytes(container,blobname,bytesContent);
You can clone / download this repository. Here is the link: