I am trying to upload a file to Windows Azure blob storage. From my understanding, you can upload files into something similar to a subdirectory. I want to upload a file to a blob such that the file essentially resides in /TestContainer/subDirectory1/subDirectory2/file.png
// Setup the Windows Aure blob client
CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("BlobStorage");
CloudBlobClient client = storageAccount.CreateCloudBlobClient();
// Retrieve the TestContainer container from blob storage
CloudBlobContainer container = client.GetContainerReference("TestContainer");
if (container.CreateIfNotExist())
container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
// Setup the blob
CloudBlob blob = container.GetBlobReference("");
// Create the meta data for the blob
NameValueCollection metadata = new NameValueCollection();
metadata["id"] = fileID.ToString();
blob.Metadata.Add(metadata);
// Store the blob
byte[] bytes = GetFileBytes();
blob.UploadByteArray(bytes);
How do I upload a file with a directory structure? The link here mentions there’s a way to do it. However, it doesn’t show you how to do it.
Thank you!
There are a couple of ways. The easier way is to just use
/characters as @makerofthings7 already mentioned. You can also use aCloudBlobDirectoryobject if you prefer. Here’s an example showing both.