gpt4 book ai didi

c# - 通过内存流从Azure blob存储下载文件

转载 作者:行者123 更新时间:2023-12-02 06:10:07 25 4
gpt4 key购买 nike

目前,我的 .net 7 API 中有一个端点,当命中该端点时,应使用文件名从我的 Blob 存储中下载特定文件。当我测试端点时,我收到 500 错误,指出“此流不支持超时”。我已附上下面的错误以获取更多信息。

端点.cs

public IEndpointRouteBuilder MapEndpoints(IEndpointRouteBuilder endpoints)
{


endpoints.MapGet("getBlob/{blobname}", async (string blobName, IApplicationFormService applicationFormService) => await applicationFormService.GetBlob(blobName))
.Produces<FileStreamResult>();


return endpoints;
}

服务.cs

public async Task<IResult> GetBlob(string blobName)
{

BlobClient blobClient = _blobServiceClient
.GetBlobContainerClient("root")
.GetBlobClient(blobName);


try
{
using (var memoryStream = new MemoryStream())
{
await blobClient.DownloadToAsync(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);


var newMemoryStream = new MemoryStream(memoryStream.ToArray());


return Results.Ok(new FileStreamResult(newMemoryStream, "application/octet-stream") { FileDownloadName = blobName });
}

}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}



return Results.Ok(blobName);
}

enter image description here

最佳答案

问题是当我应该返回 Results.File() 时我却返回了 Results.ok()

using (var memoryStream = new MemoryStream())
{
await blobClient.DownloadToAsync(memoryStream);
//memoryStream.Seek(0, SeekOrigin.Begin);

return Results.File(memoryStream.ToArray(), "application/octet-stream", blobName);
}

关于c# - 通过内存流从Azure blob存储下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75606797/

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