In this new thread, we are going to work with Azure Blob Storage using Python. The first part covers installing the Python SDK and uploading our first set of data with some metadata.
To install the Python SDK for Azure, run the following command:
pip install azure
Alternatively, if you prefer to download it from GitHub:
git clone git://github.com/Azure/azure-sdk-for-python.git
cd azure-sdk-for-python
python setup.py install
Now that you have installed the requirements, let's structure our script. Here's the recommended approach:
Start your script with the following imports:
import os
import sys
import time
import azure
from azure.storage.blob import BlockBlobService
from azure.storage.blob import ContentSettings
Optionally, for advanced features like threading and JSON handling:
import traceback
import threading
import json
from decimal import *
from datetime import datetime
Set up your Azure storage account credentials:
AccountName = "rgcloudmouradgeneralpurp"
AccountKey = "***********************NjfXoQk3luKV/UhKm*****TTc6JgzHdi5mO/x2V*******=="
Create your storage service and define the target container:
MyFile_to_Upload = "test.txt"
block_blob_service = BlockBlobService(account_name=AccountName,
account_key=AccountKey)
container_source = "mouradpubliccontainer"
Create metadata for your blob (a collection of key-value strings):
metadata_loaded = {
'Owner': 'foo',
'Dept': 'blah',
'Environment': 'Naboo',
'Customer': 'Jabbah',
'Project': 'StarWars'
}
Create the function that handles the upload:
def upload_func(container, blobname, filename, MetaDataS):
start = time.clock()
block_blob_service.create_blob_from_path(
container,
blobname,
filename,
metadata=MetaDataS
)
elapsed = time.clock() - start
print("*** DEBUG *** Time spent uploading: ", filename,
" is: ", elapsed, " in container: ", container)
In this function:
container: The target container nameblobname: The name the blob will have once stored in Azurefilename: The local file to be uploadedMetaDataS: The metadata to associate with the blobFinally, call your function to start the upload:
upload_func(container_source, MyFile_to_Upload, MyFile_to_Upload, metadata_loaded)
That's it! Check your Azure storage account portal - your file should now be uploaded to the container with the metadata attached.