通过 Azure 函数流式传输 blob 内容 - HttpResponseMessage 对象而不是内容被序列化

问题描述 投票:0回答:1

我正在尝试通过 Azure 函数将内容从 Blob 存储流式传输到浏览器。我的希望是将内容流式传输到客户端,而不是将整个 blob 内容缓冲在内存中。

我有以下代码:

public async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = route)] HttpRequestData req)
{
    var blobServiceClient = new BlobServiceClient(CONNECTION_STRING);
    var container = blobServiceClient.GetBlobContainerClient(CONTAINER_NAME);
    var blob = container.GetBlobClient(BLOB_NAME);
    var stream = await blob.OpenReadAsync();
    var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(stream) };
    response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
    response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = blob.Name };

    return response;
}

但是每次我调用该函数时,我都会收到如下文本响应:

{
  "version": "1.1",
  "content": {
    "headers": [
      {
        "key": "Content-Type",
        "value": [
          "application/octet-stream"
        ]
      },
      {
        "key": "Content-Disposition",
        "value": [
          "attachment; filename=\"BLOB_NAME\""
        ]
      }
    ]
  },
  "statusCode": "OK",
  "reasonPhrase": "OK",
  "headers": [],
  "trailingHeaders": [],
  "requestMessage": null,
  "isSuccessStatusCode": true
}

如您所见,这看起来像

HttpResponseMessage
对象已被序列化并传输,而不是内容。

出了什么问题?为什么 blob 内容本身没有被传输?

c# download azure-functions azure-blob-storage blob
1个回答
0
投票

通过 Azure 函数流式传输 blob 内容 - HttpResponseMessage 对象而不是内容被序列化

我在 dotnet 中使用 http 触发器创建了 azure 函数。 我创建了 azure blob 存储并将一个 blob 上传到容器中。检查下面:

enter image description here

通过使用下面的代码,我能够在本地环境中获取 blob 详细信息。

功能代码:

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage;
using System.Threading.Tasks;

public static class GetBlobDetails
{
    [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
        ILogger log)
    {
        try
        {
            string storageConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("Your-container-name");

            List<string> blobNames = new List<string>();
            BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(null);
            foreach (IListBlobItem item in resultSegment.Results)
            {
                if (item is CloudBlob blob)
                {
                    blobNames.Add(blob.Name);

                    // Log additional blob details to the console
                    log.LogInformation($"Blob Name: {blob.Name}");
                    log.LogInformation($"Blob Uri: {blob.Uri}");
                    log.LogInformation($"Blob Properties: LastModified={blob.Properties.LastModified}, ContentType={blob.Properties.ContentType}");
                }
            }

            // Log the blob names to the console
            log.LogInformation($"Blob Names in Container: {string.Join(", ", blobNames)}");

            return new OkObjectResult($"Blob Names in Container: {string.Join(", ", blobNames)}");
        }
        catch (Exception ex)
        {
            log.LogError($"Error: {ex.Message}");
            return new StatusCodeResult(StatusCodes.Status500InternalServerError);
        }
    }
}

local.settiongs.json:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "your-azure storage conn-string",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

输出:

enter image description here enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.