首页 > 解决方案 > 如何在 Azure 函数中设置 blob 属性?

问题描述

此示例说明如何设置 blob 属性,例如ContentType使用 C#。如何在以下 Azure 函数中完成此操作?方法签名不使用CloudBlob对象,而是使用Stream对象来读取 blob。

[FunctionName("MyFunction")]
public static async Task Run([BlobTrigger("container-name/folder-name/{name}", Connection = "ConnectionString")]Stream myBlob, string name, ILogger log, Binder binder)
{
    // How to change the ContentType property?
}

标签: c#azureazure-functionsazure-blob-storage

解决方案


请使用以下代码(我使用的是 Visual Studio 2017,并创建函数 v2):

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;

namespace FunctionApp3
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([BlobTrigger("container-name/folder-name/{name}", Connection = "AzureWebJobsStorage")]ICloudBlob myBlob, string name, ILogger log)
        {
            log.LogInformation("...change blob property...");

            //specify the property here
            myBlob.Properties.ContentType = "text/html";

            //commit the property
            myBlob.SetPropertiesAsync();
        }
    }
}

推荐阅读