Azure Blob存储将元数据从上载传递到Blob触发器

时间:2018-11-29 14:25:35

标签: c# azure azure-blob-storage

我正在开发一组Azure函数来执行此高级过程

  • 通过Http触发器接收HTTP请求
  • 将这些请求中的元数据保存到表存储中
  • 生成PDF,并将其上传到Blob存储
  • 更新表存储中的记录以包含对文档的引用

除了最后一部分之外,所有内容都可以正常工作。

将PDF上载到blob存储中后,我们可以访问请求元数据,但是blob触发器只能访问上载的PDF流。

我尝试了以下操作:

文档上传

 var context = new OperationContext();
 context.UserHeaders = new Dictionary<string, string>();
 context.UserHeaders.Add("RowKey", entity.RowKey);
 context.UserHeaders.Add("PartitionKey", entity.PartitionKey);
 cloudBlockBlob.UploadFromStream(downloadedStream, operationContext: context);

Blob触发私有方法

public static void Run(
        [BlobTrigger(FunctionConstants.PrintSetToBlobName, Connection = ConnectionStringName)]Stream myBlob, 
        string name, 
        TraceWriter log)
{
     // code truncated for clarity
     string documentUrl = GetDocumentUrl();
     UpdateEntity(documentUrl);
}

private static void UpdateEntity(string documentUrl)
{
        // This doesn't seem like it will work
        OperationContext context = new OperationContext();
        EntityService entityService = new EntityService();

        context.UserHeaders.TryGetValue("RowKey", out string rowKey);
        context.UserHeaders.TryGetValue("PartitionKey", out string partitionKey);

        var entity = entityService.Get(rowKey, partitionKey);

        entity.DocumentUrl = documentUrl;
        entityService.Update(entity);
}

我尝试直接在{em> BlobTrigger.Run 函数中将OperationContext作为参数传递,但由于它不是有效输入而导致运行时错误。

最终,我的问题是如何从 CloudBlockBlob.UploadFromStream 方法传递元数据并在Blob触发器中读取它?

1 个答案:

答案 0 :(得分:2)

UserHeaders设置请求的HTTP标头,以将Blob上传到Azure存储。这些标头在BlobTrigger中不可用,因此您不能使用它们传递其他参数。

您可以将PartitionKeyRowKey保存到Blob元数据:

cloudBlockBlob.Metadata["PartitionKey"] = entity.PartitionKey;
cloudBlockBlob.Metadata["RowKey"] = entity.RowKey;
cloudBlockBlob.SetMetadata();

在您的其他函数中,将BlobTrigger绑定到CloudBlockBlob

public static void Run(
    [BlobTrigger(FunctionConstants.PrintSetToBlobName, Connection = ConnectionStringName)]CloudBlockBlob cloudBlockBlob, 
    string name, 
    TraceWriter log)
{
    ...
} 

并读取元数据

cloudBlockBlob.FetchAttributes();
var partitionKey = cloudBlockBlob.Metadata["PartitionKey"];
var rowKey = cloudBlockBlob.Metadata["RowKey"];
相关问题