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

标签 c# azure api .net-core .net-7.0

目前,我的 .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/

相关文章:

azure - PowerShell错误: Set-AzureDeployment : CurrentStorageAccountName is not set

API REST Yammer : cant retrieve admins from a group

api - Paypal 创建沙盒交易

objective-c - 在 REST Api 中建模对象继承

azure - 在哪里可以找到 Azure Web 应用程序的每日总计算时间

c# - 如何使用 LINQ 从 IEnumerable<T> 的属性中获取单个 IENumerable<X>?

c# - 如何将数据导出到 Excel 文件

c# - c#中的模拟文件IO静态类

c# - Word Automation多重粘贴问题

asp.net - 如何启用连接:Keep-Alive for aps.net core 2.1