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.

Installation

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

Script Structure

Now that you have installed the requirements, let's structure our script. Here's the recommended approach:

  1. Import the correct libraries
  2. Define credentials, artifacts and targets
  3. Define your Python function
  4. Call your function

Step 1: Import Libraries

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

Step 2: Define Credentials and Artifacts

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"

Step 3: Add Metadata

Create metadata for your blob (a collection of key-value strings):

metadata_loaded = {
    'Owner': 'foo', 
    'Dept': 'blah', 
    'Environment': 'Naboo',
    'Customer': 'Jabbah',
    'Project': 'StarWars'
}

Step 4: Define Your Upload Function

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 name
  • blobname: The name the blob will have once stored in Azure
  • filename: The local file to be uploaded
  • MetaDataS: The metadata to associate with the blob

Step 5: Execute the Function

Finally, 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.