简体   繁体   English

将Azure容器下载为zip文件asp.net mvc

[英]download azure container as zip file asp.net mvc

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 我想将天蓝色的容器中的所有文件下载为zip文件,并使下载路径动态化,这是现在的代码

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. 您需要完成3个步骤来完成工作。

Step 1, Download all the files to a folder. 步骤1,将所有文件下载到文件夹。 I suggest you create a folder under your content folder of your web application. 建议您在Web应用程序的内容文件夹下创建一个文件夹。

// 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. 步骤2,下载文件后,我们可以使用System.IO.Compression.ZipFile类压缩文件夹。 To use it, we need to add references to 2 assemblies. 要使用它,我们需要添加对2个程序集的引用。 System.IO.Compression and System.IO.Compression.FileSystem. System.IO.Compression和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. 步骤3,由于zip文件是在内容文件夹中生成的,因此您可以生成用于下载操作的URL。

string url = "http://hostname:port/content/" + containerName + currentDateTime + ".zip"; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM