python script: importing a dir tree into windows azure storage | blogatom.

I have written a quick Python script to upload a whole directory tree full of files into Windows Azure storage. I used it to upload all the static files from my previous WordPress installation into Blobs. It uses the simple Python winazurestorage script that can be found on Github.

The script uses os.walk() to find all the files and the mimetypes module to guess at the MIME content type for the file. The call to put_blob() will fail if you do not include a Content-Type.

#!/usr/bin/env python

import sys import mimetypes

sys.path.append("/Users/tom/Documents/winazurestorage")

from winazurestorage import *

BLOB_SERVER = "blob.core.windows.net" BLOB_ACCOUNT = "youraccount" BLOB_KEY = "yourkey"

DEST_CONTAINER = "images"

blobs = BlobStorage(BLOB_SERVER, BLOB_ACCOUNT, BLOB_KEY) print blobs.create_container(DEST_CONTAINER)

mimetypes.init()

for root, dirs, files in os.walk("."): if '.DS_Store' in files: files.remove('.DS_Store') for file in files: filename = os.path.join(root, file) type, encoding = mimetypes.guess_type(filename) blobname = filename.lstrip('.') print "uploading ", filename, type f = open(filename, "rb") data = f.read() print blobs.put_blob(DEST_CONTAINER, blobname, data, type) f.close()