i want to download all files in container on azure as zip file and make the path to download be dynamic this the right now code
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string[] arr = userName.Split('\\');
string path = $@"C:\Users\{arr[1]}\Downloads\";
CloudBlobContainer contianner = BlobClient.GetContainerReference(contianerName);
var list = contianner.ListBlobs();
/// Console.WriteLine(list.Count());
string[] FilesName = new string[list.Count()];
int i = 0;
foreach (var blob in list)
{
string[] Name = blob.Uri.AbsolutePath.Split('/');
FilesName[i++] = Name[2];
// Console.WriteLine(Name[2]);
CloudBlockBlob blockBlob = contianner.GetBlockBlobReference(Name[2]);
System.IO.Directory.CreateDirectory($@"{path}ImagesPath");
using (var fileStream = System.IO.File.OpenWrite($@"{path}\ImagesPath\{Name[2]}"))
{
blockBlob.DownloadToStream(fileStream);
}
}
You need to finish your job using 3 steps.
Step 1, Download all the files to a folder. I suggest you create a folder under your content folder of your web application.
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("storage connection string");
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
string containerName = "mycontainer";
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
var blobs = container.ListBlobs(string.Empty, true);
string currentDateTime = DateTime.Now.ToString("yyyyMMddhhmmss");
string directoryPath = Server.MapPath("~/Content/" + containerName + currentDateTime);
System.IO.Directory.CreateDirectory(directoryPath);
foreach (CloudBlockBlob blockBlob in blobs)
{
string[] segements = blockBlob.Name.Split('/');
string subFolderPath = directoryPath;
for (int i = 0; i < segements.Length - 1; i++)
{
subFolderPath = subFolderPath + "\\" + segements[i];
if (!System.IO.Directory.Exists(subFolderPath))
{
System.IO.Directory.CreateDirectory(subFolderPath);
}
}
string filePath = directoryPath + "\\" + blockBlob.Name;
blockBlob.DownloadToFile(filePath, System.IO.FileMode.CreateNew);
}
Console.WriteLine("Download files successful.");
Step 2, After download the files, we could compress the folder using System.IO.Compression.ZipFile class. To use it, we need to add references to 2 assemblies. System.IO.Compression and System.IO.Compression.FileSystem.
System.IO.Compression.ZipFile.CreateFromDirectory(directoryPath, directoryPath + ".zip");
Console.WriteLine("Compress the folder successfully");
Step 3, Since the zip file is generated in the content folder, you could generate the URL for downloading operation.
string url = "http://hostname:port/content/" + containerName + currentDateTime + ".zip";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.