如何在queuetrigger绑定中捕获子字符串并将其用于输入Blob绑定?

时间:2019-05-20 20:45:30

标签: c# .net azure-storage azure-functions azure-storage-blobs

如何使用队列消息的正文绑定到输入的blob?

我需要捕获的值为myj.json

我有一个队列触发功能:

[FunctionName ("OnSaveXmlBytesBlobTriggered")]
public static void Run (
    [QueueTrigger ("xmlinputqueue", Connection = "mystorconn")] JObject myQueueItem, 
    [Blob ("xmlinput/{jsonFileName}", FileAccess.Read, Connection = "mystorconn")] Stream blobInput,
    ILogger log) {

    log.LogInformation ($"C# Queue trigger function processed: {myQueueItem.subject}");
}

以下是myQueueItem的示例:

{
    "topic": "/subscriptions/xxxxxxxxxxx/resourceGroups/myresourcegroup/providers/Microsoft.Storage/storageAccounts/mystoraccount",
    "subject": "/blobServices/default/containers/xmlinput/blobs/myj.json",
    "eventType": "Microsoft.Storage.BlobCreated",
    "eventTime": "2019-05-20T18:58:28.7390111Z",
    "id": "xxxxxxxxxxxxxxxx",
    "data": {
        "api": "PutBlockList",
        "clientRequestId": "xxxxxxxxxxxxxxxx",
        "requestId": "xxxxxxxxxxxxxxxx",
        "eTag": "0x8D6DD55254EBE75",
        "contentType": "application/json",
        "contentLength": 874636,
        "blobType": "BlockBlob",
        "url": "https://mystoraccount.blob.core.windows.net/xmlinput/myj.json",
        "sequencer": "00000000000000000000000000005FAC0000000000614963",
        "storageDiagnostics": {
            "batchId": "xxxxxxxxxxxxxxxx"
        }
    },
    "dataVersion": "",
    "metadataVersion": "1"
}

如何在队列有效负载主体内部创建一个字符串,以便绑定到我的输入blob?

1 个答案:

答案 0 :(得分:1)

您将需要将JToken转换为字符串,搜索最后一个斜杠位置,然后将其前面的所有内容都字符串化。

string url = myQueueItem["data"]["url"].ToString();
int pos = url.LastIndexOf("/") + 1;
Console.WriteLine(url.Substring(pos, url.Length - pos));

将返回myj.json