【问题标题】:Blob access from event grid azure function using Java API使用 Java API 从事件网格 Azure 函数访问 Blob
【发布时间】:2019-10-02 16:37:44
【问题描述】:

背景

Java Azure 函数 2,使用带有事件网格订阅的 blob 存储来创建函数(见下文)通过事件触发器绑定到的事件。

问题

不清楚如何绑定 blob(参见 @BlobInput Java 注释)输入 Blob 绑定来自 documentation 所暗示的 Azure 函数,但不确定在 Java API 中是否可能,不像它的 C# 对应物。

当函数被调用时,没有使用@BlobInput 注释绑定到content 变量,所以一旦到达content 变量所在的行,它就会产生一个空指针。

基于文档的path = "{data.url}" 允许您访问传递给函数的事件数据。从事件传递的数据也都绑定到EventSchema event POJO(参见下面的事件示例)。

@StorageAccount("AzureWebJobsStorage")通过函数配置默认链接到存储和设置的属性,这是正确的。

试过

部署的天蓝色功能:

@StorageAccount("AzureWebJobsStorage")
@FunctionName("myfunc")
public void run(@EventGridTrigger(name = "blobeventgrid") EventSchema event,
                @BlobInput(name = "zipfile",dataType = "binary",path = "{data.url}") byte[] content,
                final ExecutionContext context) {
    context.getLogger().info("Java Event Grid trigger function executed.");
    context.getLogger().info("Id: " + event.id);
    context.getLogger().info("Data: " + event.data);
    context.getLogger().info("zip file: " + content.length);
}

示例事件网格事件

{
  "topic": "/subscriptions/<omitted>/resourceGroups/java-functions-group/providers/Microsoft.Storage/storageAccounts/<omitted storageaccount>",
  "subject": "/blobServices/default/containers/mycontainer/blobs/compressed.zip",
  "eventType": "Microsoft.Storage.BlobCreated",
  "eventTime": "2019-10-02T12:46:33.2915427Z",
  "id": "<omitted>",
  "data": {
  "api": "PutBlob",
  "clientRequestId": "<omitted>",
  "requestId": "<omitted>",
  "eTag": "<omitted>",
  "contentType": "application/zip",
  "contentLength": 32460,
  "blobType": "BlockBlob",
  "url": "https://<omitted storageaccount>.blob.core.windows.net/mycontainer/compressed.zip",
  "sequencer": "<omitted>",
  "storageDiagnostics": {
  "batchId": "<omitted>"
    }
  },
  "dataVersion": "",
  "metadataVersion": "1"
}

本地运行函数的日志 (远程相同)

[10/05/2019 18:48:16] Executing HTTP request: {
[10/05/2019 18:48:16]   "requestId": "299a3870-98cf-41cf-b418-7cdb33c1f1c7",
[10/05/2019 18:48:16]   "method": "POST",
[10/05/2019 18:48:16]   "uri": "/runtime/webhooks/EventGrid"
[10/05/2019 18:48:16] }
[10/05/2019 18:48:17] Executing 'Functions.myFunc' (Reason='EventGrid trigger fired at 2019-10-05T19:48:17.4343990+01:00', Id=82a2f47b-34bc-492f-8b60-12601beb45ee)
[10/05/2019 18:48:18] Java Event Grid trigger function executed.
[10/05/2019 18:48:18] Event content 
[10/05/2019 18:48:18] Subject: /blobServices/default/containers/mycontainer/blobs/zip/compressed.zip
[10/05/2019 18:48:18] Time: Mon Sep 30 20:46:33 BST 2019
[10/05/2019 18:48:18] Id: 7de5edc4-c01e-0107-1bc7-77755f061e49
[10/05/2019 18:48:18] Data: {api=PutBlob, clientRequestId=007dd554-e3bb-11e9-80b4-dca90473b192, requestId=7de5edc4-c01e-0107-1bc7-77755f000000, eTag=0x8D745DEE5936EE3, contentType=application/zip, contentLength=32460.0, blobType=BlockBlob, url=https://<ommitted storage account>.blob.core.windows.net/mycontainer/zip/compressed.zip, sequencer=000000000000000000000000000007E200000000002ab872, storageDiagnostics={batchId=1c15a3b6-2006-0046-00c7-771b19000000}}
[10/05/2019 18:48:18] Executed 'Functions.myFunc' (Failed, Id=82a2f47b-34bc-492f-8b60-12601beb45ee)
[10/05/2019 18:48:18] System.Private.CoreLib: Exception while executing function: Functions.myFunc. System.Private.CoreLib: Result: Failure
[10/05/2019 18:48:18] Exception: NullPointerException: 

因为没有内容绑定到内容字节[]...

替代方案

使用 Azure Java SDK,但尝试保持围绕 Azure 函数的语义。

【问题讨论】:

  • 您的 StorageAccount("AzureWebJobsStorage") 是否与 相同?如果不是,请使用@BlobInput 中的connection。顺便提一句。使用 C# 运行良好。
  • 更新帖子以解决您的问题,谢谢
  • 用于测试目的:添加行:context.getLogger().info("Url:" + event.data.url);
  • 事件有效,这不是我的问题,我可以访问事件对象中的任何内容...问题在于 Blob 输入
  • 那么,你可以在这个添加的行中看到url地址,是吗?我使用的是 C#,这就是为什么我有这些故障排除问题,否则我会在我的环境中进行。另一个故障排除问题:尝试用事件对象中的 data.url 的显式值替换绑定,以证明 BlobInput 绑定。​​

标签: java azure azure-functions azure-blob-storage azure-eventgrid


【解决方案1】:

你的函数几乎是正确的。根据我的测试,{data.url} 的值将是一个 http url,如下所示:

https://storagetest789.blob.core.windows.net/test/test.txt

如果您设置了正确的存储连接字符串,绑定将起作用并且您将获得内容。

这是我的验证:

1。代码

public class Function {

    @FunctionName("StroageEventGrid")
    @StorageAccount("AzureWebJobsStorage")
    public void run(@EventGridTrigger(name = "blobeventgrid") EventSchema event, 
                    @BlobInput(name = "blob",dataType = "binary",path = "{data.url}") byte[] content,
                    final ExecutionContext context) 
    {
        context.getLogger().info((String)event.data.get("url"));
        if(content != null)
            context.getLogger().info("Length: " + content.length);
        else
            context.getLogger().info("Content is null");
    }
}

public class EventSchema {

  public String topic;
  public String subject;
  public String eventType;
  public Date eventTime;
  public String id;
  public String dataVersion;
  public String metadataVersion;
  public Map<String, Object> data;

}

2。检查应用程序设置中的 AzureWebJobsStorage 连接字符串

确保它是目标存储帐户的正确连接字符串。

3。上传新的 Blob

你可以看到我得到了正确的结果。

所以,我建议您检查您的连接字符串设置。默认情况下,您的本地测试设置不会更新到云端,这可能会导致此问题。

【讨论】:

  • 我很早就意识到了这一点,因为指南告诉您参考默认连接字符串。我只是来这里回答我自己的问题,但你已经打败了我。感谢您花时间回答,我相信这将在不久的将来帮助很多人。
猜你喜欢
  • 2021-06-01
  • 2018-04-19
  • 2023-01-25
  • 2021-12-10
  • 2020-12-12
  • 2021-09-17
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
相关资源
最近更新 更多