c# - 下载文件后如何从 azure blob 存储中删除文件

标签 c# asp.net-mvc azure .net-core azure-blob-storage

我正在开发一个存储我的文件的 blob 存储系统。 现在,我可以从 Blob 容器中删除/取消删除文件。 我正在尝试将文件从 Controller 下载回浏览器并删除该文件。

这是我的下载 Controller

    public ActionResult DownloadBlob(string name) {
            CloudBlobContainer container = GetCloudBlobContainer();
            var resultSegment = container.ListBlobsSegmentedAsync(name.Split('/')[0],true ,BlobListingDetails.All,null,null,null,null).Result;
            CloudBlockBlob target = (CloudBlockBlob)resultSegment.Results.FirstOrDefault(e => e.Uri.Segments.Last() == name.Split('/')[1]);
            //var directory = container.GetDirectoryReference(name.Split('/')[0]);
            //var block = directory.GetBlockBlobReference(name.Split('/')[1]);

            if (target.ExistsAsync().Result) {
            } else {
                target.UndeleteAsync().Wait();
            }

            Stream stream = target.OpenReadAsync().Result;
            string contentType = target.Properties.ContentType;
            ;

            target.DeleteIfExistsAsync();

            return new FileStreamResult(stream, contentType) {
                FileDownloadName = "Downloaded_" + name.Split('/')[1]
            };
    }

所以如果我有一个已删除的文件,我想取消删除它,下载它然后再次删除它。(软删除已打开) 有没有办法做到这一点,以便删除在 return 语句之后执行

最佳答案

您当前的代码中是否遇到“找不到 blob”错误?如果是, 您可以使用MemoryStream和blob.DownloadToStream(memoryStream),那么您可以在下载完成后删除blob,无需在return语句后调用delete。

我安装了这个blob存储的nuget包:Microsoft.Azure.Storage.Blob, Version 11.0.0 ,它支持异步和非异步 blob 方法。您可以使用此包或根据评论将当前代码更改为异步。

示例代码在我这边运行良好(测试代码,您可以随意修改以满足您的需要):

    public IActionResult Contact()
    {
        string account_name = "xx";
        string account_key = "xx";

        CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(account_name, account_key), true);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference("test1");
        CloudBlockBlob blob = blobContainer.GetBlockBlobReference("df1.JPG");

        if (!blob.Exists())
        {
            blob.Undelete();
        }

        MemoryStream memoryStream = new MemoryStream();

        blob.DownloadToStream(memoryStream);
        memoryStream.Position = 0;
        string contentType = blob.Properties.ContentType;

       blob.DeleteIfExists();

        return new FileStreamResult(memoryStream, contentType)
        {
            FileDownloadName = blob.Name
        };

    }

关于c# - 下载文件后如何从 azure blob 存储中删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57398904/

相关文章:

c# - asp.net response.write 与 bootbox.alert

c# - 如何使用正则表达式从字符串中获取精确的 8 位数字?

c# - Xamarin.forms 中的 UITests 发布失败

c# - 从远程计算机以编程方式检索 Windows 事件 - Windows XP

c# - umbraco 中的 URL 重定向,当找不到文档时,重定向到其他地方

c# - ASP.NET MVC Cookie 实现

azure - 如何通过 Azure 门户连接到 VPN 内的数据库服务器?

azure - 使用 Solr 在 Azure VM 上构建专用搜索服务器

asp.net - 使用 Webforms 和 MVC 路由到项目上的 aspx 页面

azure - 如何从azure devops构建服务器清理docker镜像