目前,我的 .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);
}
最佳答案
问题是当我应该返回 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/