首页 > 解决方案 > 如何更改输出 Blob 流的内容类型?

问题描述

我有一个函数可以获取一个 blob,调整它的大小并将其输出到另一个容器。

  public static void Run(
            [BlobTrigger("test/{name}")] Stream image,
            string name,
            [Blob("test-tn/{name}", FileAccess.Write)] Stream imageSmall, ILogger log)

然而,生成的图像将始终具有流的内容类型,我想定义内容类型。

我假设我可以输出 CloudBlockBlob 而不是流,但我总​​是收到此错误:

Can't bind Blob to type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob

为 BlobTrigger 的输出定义内容类型的最佳方法是什么?

标签: azure-functions

解决方案


我认为您问题的根源在于您使用的是过时版本的 Storage nuget 包。有关Blob 输出的完整示例,请参见此处:

.csproj文件片段:

<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="4.0.3" />

Http2BlobFunction.cs

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

namespace SampleFunctions
{
    public static class Http2BlobFunction
    {
        [FunctionName(nameof(Http2Blob))]
        public static async Task<IActionResult> Http2Blob(
            [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
            [Blob("myblobcontainer/{rand-guid}.txt", FileAccess.ReadWrite)] CloudBlockBlob blob,
            ILogger log)
        {
            log.LogInformation("Received file upload request");
            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            await blob.UploadTextAsync(requestBody);
            return new OkObjectResult(blob.Name);
        }
    }
}

推荐阅读