Get azure blob files inside Sub Directories
In Microsoft doc's we can find code to get blob files inside a container using c#. What if Container has some Sub Directories with blob data/files. How we can replicate the folder structure in our local machine.
public string storageAccountName = "azure storage account name";
public string accessKey = "your azure accesskey";
public string containerName = "myfirstblobcontainer";
public string connectiongString = "Azure connection string";
public string rootPath = @"C:\Users\Home\Downloads\your folder Name";
protected void Page_Load(object sender, EventArgs e)
{
CloudStorageAccount storageAccount = new CloudStorageAccount(
new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, accessKey), true);
// Create a blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Get a reference to a container named "mycontainer."
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
// If "mycontainer" doesn't exist, create it.
container.CreateIfNotExistsAsync();
// Get a reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
GetBlobData(container);
}
public void GetBlobData(CloudBlobContainer container)
{
// Loop over items within the container and output the length and URI.
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
DownloadBlobAsync(container, item);
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob pageBlob = (CloudPageBlob)item;
}
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory directory = (CloudBlobDirectory)item;
ListBlobsOfDirectory(container, directory);
}
else
{
CloudBlobDirectory directoryFiles = (CloudBlobDirectory)item;
}
}
}
private void ListBlobsOfDirectory(CloudBlobContainer container, CloudBlobDirectory directory)
{
foreach (IListBlobItem item in directory.ListBlobs())
{
var type = item.GetType();
if (type == typeof(CloudBlockBlob))
{
//Trace.TraceInformation(obj.Uri.ToString());
DownloadBlobAsync(container, item);
}
else if (type == typeof(CloudBlobDirectory))
{
ListBlobsOfDirectory(container, (CloudBlobDirectory)item);
}
}
}
public void DownloadBlobAsync(CloudBlobContainer container, IListBlobItem item)
{
CloudBlockBlob blob = (CloudBlockBlob)item;
string fileCompletePath = rootPath;
// Get a reference to a blob named "photo1.jpg".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blob.Name);
string fileName = blob.Name;
if (fileName.Contains(@"/"))
{
string[] folderNames = fileName.Split('/');
int folderCount = folderNames.Length;
//for two directories
if (folderNames.Length == 2)
{
string folderName = folderNames[0];
CreateIfMissing(rootPath + @"\" + folderName);
fileCompletePath = rootPath + @"\" + folderName;
CreateFile(blockBlob, fileCompletePath + @"\" + folderNames[folderCount - 1]);
}
//For sub folders
else if (folderNames.Length > 2)
{
string pathToBeCreate = string.Empty;
////Here we are creating subfolders then creating file.
for (int i = 0; i < folderNames.Length - 1; i++)
{
string folderName = folderNames[i];
pathToBeCreate += i == 0 ? folderName : @"\" + folderName;
CreateIfMissing(rootPath + @"\" + pathToBeCreate);
}
fileCompletePath = rootPath + @"\" + pathToBeCreate;
CreateFile(blockBlob, fileCompletePath + @"\" + folderNames[folderCount - 1]);
}
}
else
{
CreateFile(blockBlob, fileCompletePath + @"\" + blockBlob.Name);
}
}
private void CreateFile(CloudBlockBlob blockBlob, string filePath)
{
//Save the blob contents to a file named
using (var fileStream = System.IO.File.OpenWrite(filePath))
{
blockBlob.DownloadToStream(fileStream);
}
}
private void CreateIfMissing(string path)
{
bool folderExists = Directory.Exists(path);
if (!folderExists)
{
Directory.CreateDirectory(path);
}
}
There might a change Container contains empty folder or a file. Below code snippet will help to create the same Azure Container structure. here i have used ASPX pages as per my requirement. we can use other applications as well.
To get Azure connection string use below URL:
public string accessKey = "your azure accesskey";
public string containerName = "myfirstblobcontainer";
public string connectiongString = "Azure connection string";
public string rootPath = @"C:\Users\Home\Downloads\your folder Name";
protected void Page_Load(object sender, EventArgs e)
{
CloudStorageAccount storageAccount = new CloudStorageAccount(
new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, accessKey), true);
// Create a blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Get a reference to a container named "mycontainer."
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
// If "mycontainer" doesn't exist, create it.
container.CreateIfNotExistsAsync();
// Get a reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
GetBlobData(container);
}
public void GetBlobData(CloudBlobContainer container)
{
// Loop over items within the container and output the length and URI.
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
DownloadBlobAsync(container, item);
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob pageBlob = (CloudPageBlob)item;
}
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory directory = (CloudBlobDirectory)item;
ListBlobsOfDirectory(container, directory);
}
else
{
CloudBlobDirectory directoryFiles = (CloudBlobDirectory)item;
}
}
}
private void ListBlobsOfDirectory(CloudBlobContainer container, CloudBlobDirectory directory)
{
foreach (IListBlobItem item in directory.ListBlobs())
{
var type = item.GetType();
if (type == typeof(CloudBlockBlob))
{
//Trace.TraceInformation(obj.Uri.ToString());
DownloadBlobAsync(container, item);
}
else if (type == typeof(CloudBlobDirectory))
{
ListBlobsOfDirectory(container, (CloudBlobDirectory)item);
}
}
}
public void DownloadBlobAsync(CloudBlobContainer container, IListBlobItem item)
{
CloudBlockBlob blob = (CloudBlockBlob)item;
string fileCompletePath = rootPath;
// Get a reference to a blob named "photo1.jpg".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blob.Name);
string fileName = blob.Name;
if (fileName.Contains(@"/"))
{
string[] folderNames = fileName.Split('/');
int folderCount = folderNames.Length;
//for two directories
if (folderNames.Length == 2)
{
string folderName = folderNames[0];
CreateIfMissing(rootPath + @"\" + folderName);
fileCompletePath = rootPath + @"\" + folderName;
CreateFile(blockBlob, fileCompletePath + @"\" + folderNames[folderCount - 1]);
}
//For sub folders
else if (folderNames.Length > 2)
{
string pathToBeCreate = string.Empty;
////Here we are creating subfolders then creating file.
for (int i = 0; i < folderNames.Length - 1; i++)
{
string folderName = folderNames[i];
pathToBeCreate += i == 0 ? folderName : @"\" + folderName;
CreateIfMissing(rootPath + @"\" + pathToBeCreate);
}
fileCompletePath = rootPath + @"\" + pathToBeCreate;
CreateFile(blockBlob, fileCompletePath + @"\" + folderNames[folderCount - 1]);
}
}
else
{
CreateFile(blockBlob, fileCompletePath + @"\" + blockBlob.Name);
}
}
private void CreateFile(CloudBlockBlob blockBlob, string filePath)
{
//Save the blob contents to a file named
using (var fileStream = System.IO.File.OpenWrite(filePath))
{
blockBlob.DownloadToStream(fileStream);
}
}
private void CreateIfMissing(string path)
{
bool folderExists = Directory.Exists(path);
if (!folderExists)
{
Directory.CreateDirectory(path);
}
}
Professionally written blogs are rare to find, however I appreciate all the points mentioned here. I also want to include some other writing skills which everyone must aware of.kookworkshops amsterdam
ReplyDeleteIt really helped me
ReplyDelete