gpt4 book ai didi

c# - Azure Blob 存储 : DownloadToByteArray VS DownloadToStream

转载 作者:IT王子 更新时间:2023-10-29 04:10:45 40 4
gpt4 key购买 nike

我一直在使用 Azure Blob 存储服务来保存/恢复要在 Azure 网页中托管的网页上下文中的文件。

在学习过程中我提出了两种解决方案;第一个基本上使用 DownloadToStream ,它的作用相同,但使用 FileStream 。在这种情况下,我必须先将文件写入服务器,然后再将其返回给用户。

public static Stream GetFileContent(string fileName, HttpContextBase context)
{
CloudBlobContainer container = GetBlobContainer();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
Stream fileStream = new FileStream(
context.Server.MapPath("~/App_Data/files/" + fileName), FileMode.Create);
blockBlob.DownloadToStream(fileStream);
fileStream.Close();
return File.OpenRead(context.Server.MapPath("~/App_Data/files/" + fileName));
}

public ActionResult Download(string fileName)
{
byte[] fileContent = MyFileContext.GetFileContent(fileName);
return File(fileContent, "application/zip", fileName);
}

另一方面,我使用了 DownloadToByteArray 函数,将 Blob 的内容写入用 Blob 文件大小初始化的字节数组中。

public static byte[] GetFileContent(string fileName)
{
CloudBlobContainer container = GetBlobContainer();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
blockBlob.FetchAttributes();
long fileByteLength = blockBlob.Properties.Length;
byte[] fileContent = new byte[fileByteLength];
for (int i = 0; i < fileByteLength; i++)
{
fileContent[i] = 0x20;
}
blockBlob.DownloadToByteArray(fileContent,0);
return fileContent;
}

public ActionResult Download(string fileName)
{
byte[] fileContent = MyFileContext.GetFileStream(fileName);
return File(fileContent, "application/zip", fileName);
}

当我查看这两个选项时,我发现第一个选项需要在服务器磁盘中创建文件,而第二个选项则需要将 Blob 中的数据存储在消耗内存的字节数组中。在我的特定情况下,我将处理 ~150 MB 的文件大小。

考虑到具体情况(环境、文件大小...),您认为哪种方法最好?

最佳答案

您可以直接从 Blob 存储下载它,而不是通过服务器流式传输 Blob。我的答案是建立在史蒂夫在这里的回应之上的:Downloading Azure Blob files in MVC3 。要直接从存储下载 blob,您可以使用共享访问签名 (SAS)。最近,Azure 存储引入了一项增强功能,允许您在 SAS 中指定 Content-Disposition header 。请参阅此修改后的代码。

    public static string GetDownloadLink(string fileName)
{
CloudBlobContainer container = GetBlobContainer();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
//Create an ad-hoc Shared Access Policy with read permissions which will expire in 12 hours
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(12),
};
//Set content-disposition header for force download
SharedAccessBlobHeaders headers = new SharedAccessBlobHeaders()
{
ContentDisposition = string.Format("attachment;filename=\"{0}\"", fileName),
};
var sasToken = blockBlob.GetSharedAccessSignature(policy, headers);
return blockBlob.Uri.AbsoluteUri + sasToken;
}

public ActionResult Download(string fileName)
{
var sasUrl = GetDownloadLink(fileName);
//Redirect to SAS URL ... file will now be downloaded directly from blob storage.
Redirect(sasUrl);
}

关于c# - Azure Blob 存储 : DownloadToByteArray VS DownloadToStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24312527/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com