Modelling File System in Azure Blob Storage
Microsoft Azure Blob Storage services store your blobs in to containers. But containers can not contain other containers. So this structure is flat, it is different from file system that we know from Windows. You can imagine that container is a folder, blob is a file; but sub folder is not allowed in Azure Blob Storage. However we use always subfolders in windows file system. When storing file in blob storage, we cannot use subfolders but we can model it.
Blob storage url format: http://<storage account>.blob.core.windows.net/<container>/<blob>
But we want to create url which contains sub folders such as http://<storage account>.blob.core.windows.net/<container>/<subdir1>/<subdir2>/<blob>
There is a tricky way to do it.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings.Get("StorageConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("container1"); container.CreateIfNotExists(); CloudBlockBlob blockBlob = container.GetBlockBlobReference("mypictures/otherpictures/xyz.png"); blockBlob.UploadFromFile(@"c:\xyz.png", System.IO.FileMode.Open);
Above code create a create a container (if container is not exist) which name is "container1", then create a blob which name is mypictures/otherpictures/xyz.png. As you see, our blob name contains subdirectories mypictures and otherpictures. Actually these aren't directory, these are only parts of blob name. But url of blob is "https://<storage account>.blob.core.windows.net/container1/mypictures/otherpictures/xyz.png". When we look at this url, we can think at there are 3 folders, container1 contains mypictures and mypictures contains other pictures and xyz.png file is located in otherpictures.
As you see, we model file system in Azure Blob Storage. Actually these are not sub directories
CloudBlobContainer is a class that we use for containers. (we have already use in above code) You can use GetDirectoryReference to work with virtual directories.
comments powered by Disqus