Azure 下载 blob,创建 zip 并发送给用户

本文关键字:并发 给用户 zip 创建 下载 blob Azure | 更新日期: 2023-09-27 18:36:46

我正在使用解决方案下载文件,压缩并发送给用户(Azure blobs)。

这是我到目前为止想出的:

    public void DownloadImages(DownloadImagesModel model)
    {
        if (ModelState.IsValid && model.ImageIDs != null && model.ImageIDs.Count > 0)
        {
            var currentUser = _claimService.GetCurrentClaimsIdentity(TimeZoneExtensions.GetCurrentDate());
            var images = _fileService.GetImages(currentUser.CompanyID, model.ImageIDs).ToList();
            if (images != null && images.Count > 0)
            {
                string imageContainerURL = _fileService.GetImageContainerURL(currentUser.CompanyID) + currentUser.ImageContainerToken;
                using (var zipOutputStream = new ZipOutputStream(Response.OutputStream))
                {
                    zipOutputStream.SetLevel(0); // 0 - store only to 9 - means best compression
                    Response.BufferOutput = false;
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + "zipFileName.rar");
                    Response.ContentType = "application/octet-stream";
                    foreach (var image in images)
                    {
                        if (image.ImageLinks != null)
                        {
                            List<ImageLinkModel> imageLinks = new List<ImageLinkModel>();
                            if (model.FormatID == 0)
                                imageLinks = image.ImageLinks.ToList();
                            else
                                imageLinks = image.ImageLinks.Where(m => m.FormatID == model.FormatID).ToList();
                            if (imageLinks != null && imageLinks.Count > 0)
                            {
                                //Every image has multiple blobs for different sizes
                                foreach (var link in imageLinks)
                                {
                                    CloudBlockBlob blob = _fileService.GetBlob(imageContainerURL, link.BlobName);
                                    var entry = new ZipEntry(image.ImageName)
                                    {
                                        DateTime = TimeZoneExtensions.GetCurrentDate(),
                                        Size = blob.Properties.Length
                                    };
                                    zipOutputStream.PutNextEntry(entry);
                                    //Download
                                    blob.DownloadToStream(zipOutputStream);
                                    Response.Flush();
                                    if (!Response.IsClientConnected)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    zipOutputStream.Finish();
                    zipOutputStream.Close();
                }
                Response.End();
            }
        }
    }

当我打开下载的zip文件时,我得到:

存档意外结束。

zip 应包含三个文件,但仅包含一个文件。大小也是0..

我错过了什么?

编辑 1

命名时是否应该指定.rar类型?

我不需要设置流的位置吗?

我是否正确定位了"冲洗"、"完成"、"关闭"和"结束"功能?

编辑 2

这段代码应该有效!?

    public ActionResult DownloadImages(DownloadImagesModel model)
    {
        if (ModelState.IsValid && model.ImageIDs != null && model.ImageIDs.Count > 0)
        {
            var currentUser = _claimService.GetCurrentClaimsIdentity(TimeZoneExtensions.GetCurrentDate());
            var images = _fileService.GetImages(currentUser.CompanyID, model.ImageIDs).ToList();
            if (images != null && images.Count > 0)
            {
                var cloudStorageAccount = new CloudStorageAccount(new StorageCredentials("name", "pw"), true);
                var container = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("images-1");
                using (var zipOutputStream = new ZipOutputStream(Response.OutputStream))
                {
                    foreach (var image in images)
                    {
                        if (image.ImageLinks != null)
                        {
                            List<ImageLinkModel> imageLinks = new List<ImageLinkModel>();
                            if (model.FormatID == 0)
                                imageLinks = image.ImageLinks.ToList();
                            else
                                imageLinks = image.ImageLinks.Where(m => m.FormatID == model.FormatID).ToList();
                            if (imageLinks != null && imageLinks.Count > 0)
                            {
                                zipOutputStream.SetLevel(0);
                                var blob = container.GetBlockBlobReference(imageLinks.First().BlobName);
                                var entry = new ZipEntry(imageLinks.First().BlobName);
                                zipOutputStream.PutNextEntry(entry);
                                //Download
                                blob.DownloadToStream(zipOutputStream);
                            }
                        }
                    }
                    zipOutputStream.Finish();
                    zipOutputStream.Close();
                }
                Response.BufferOutput = false;
                Response.AddHeader("Content-Disposition", "attachment; filename=" + "zipFileName.zip");
                Response.ContentType = "application/octet-stream";
                Response.Flush();
                Response.End();
                return null;
            }
        }
        return null;
    }

Azure 下载 blob,创建 zip 并发送给用户

我能够创建一个 zip 文件并使用下面的代码下载它:

    public ActionResult Download()
    {
        var cloudStorageAccount = new CloudStorageAccount(new StorageCredentials("account-name", "account-key"), true);
        var container = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("test");
        var blobFileNames = new string[] { "file1.png", "file2.png", "file3.png", "file4.png" };
        using (var zipOutputStream = new ZipOutputStream(Response.OutputStream))
        {
            foreach (var blobFileName in blobFileNames)
            {
                zipOutputStream.SetLevel(0);
                var blob = container.GetBlockBlobReference(blobFileName);
                var entry = new ZipEntry(blobFileName);
                zipOutputStream.PutNextEntry(entry);
                blob.DownloadToStream(zipOutputStream);
            }
            zipOutputStream.Finish();
            zipOutputStream.Close();
        }
        Response.BufferOutput = false;
        Response.AddHeader("Content-Disposition", "attachment; filename=" + "zipFileName.zip");
        Response.ContentType = "application/octet-stream";
        Response.Flush();
        Response.End();
        return null;
    }

我的 blob 容器是私有的,这就是从未下载文件的原因。

我实际上认为我的容器是公共的,但直到我在 Azure 存储资源管理器中浏览我的容器,我才发现容器是私有的。