Upload CSV file into Microsoft Azure storage account using python

msc picture msc · Feb 1, 2017 · Viewed 11.1k times · Source

I am trying to upload a .csv file into Microsoft Azure storage account using python. I have found C-sharp code to write a data to blob storage. But, I don't know C# language. I need to upload .csv file using python.

Is there any example of python to upload contents of CSV file to Azure storage?

Answer

msc picture msc · Feb 1, 2017

I found the solution using this reference link. My following code perfectly work for uploading and downloading .csv file.

#!/usr/bin/env python

from azure.storage.blob import BlockBlobService
from azure.storage.blob import ContentSettings

block_blob_service = BlockBlobService(account_name='<myaccount>', account_key='mykey')
block_blob_service.create_container('mycontainer')

#Upload the CSV file to Azure cloud
block_blob_service.create_blob_from_path(
    'mycontainer',
    'myblockblob.csv',
    'test.csv',
    content_settings=ContentSettings(content_type='application/CSV')
            )

# Check the list of blob
generator = block_blob_service.list_blobs('mycontainer')
for blob in generator:
    print(blob.name)

# Download the CSV file From Azure storage
block_blob_service.get_blob_to_path('mycontainer', 'myblockblob.csv', 'out-test.csv')