Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload CSV file into Microsoft Azure storage account using python

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?

like image 798
msc Avatar asked Dec 04 '22 23:12

msc


1 Answers

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')
like image 174
msc Avatar answered Dec 11 '22 16:12

msc