【问题标题】:Azure Java Function, Get HttpTrigger body attribute value in BlobOutput path annotationAzure Java 函数,在 BlobOutput 路径注释中获取 HttpTrigger 正文属性值
【发布时间】:2020-04-01 12:02:16
【问题描述】:

我有一个 HttpTrigger 函数,请求正文为

{
"id":"222",
"name":"some name"
}

我想从请求正文中获取 Id 到 @BlobOuput 路径中,如下所示

@BlobOutput(name="blob",  path="input-blob/{body.id}")

有没有办法实现这个功能。

【问题讨论】:

  • 您还有其他顾虑吗?如果您没有其他顾虑,您可以接受它作为答案吗?

标签: java azure function dynamic-binding


【解决方案1】:

关于如何从httptrigger请求体中读取@BlobOuput路径,请参考以下步骤。

我使用以下请求正文进行测试

{
"BlobName":"test.txt",
 "Content":"test"
}
  1. 创建一个自定义类作为请求正文。
public class BlobInfo {
    public String Content;
    public String BlobName;

    public String getContent() {
        return Content;
    }

    public void setContent(String content) {
        Content = content;
    }

    public String getBlobName() {
        return BlobName;
    }

    public void setBlobName(String blobName) {
        BlobName = blobName;
    }
}
  1. 功能代码
@FunctionName("HttpExample")
    public HttpResponseMessage run(
            @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<BlobInfo>> request, // request body gets automatically de-serialized into the custom object you create
            @BlobOutput(name="output",  path = "test/{BlobName}" //  use the custom object's property you need as file name
                    ,connection = "AzureWebJobsStorage") OutputBinding<String> outputItem,
            final ExecutionContext context) throws IOException {
        context.getLogger().info("Java HTTP trigger processed a request.");
        BlobInfo body = request.getBody().get();
        context.getLogger().info(body.Content);
        context.getLogger().info(body.BlobName);
        outputItem.setValue("Hello World!");

        return request.createResponseBuilder(HttpStatus.OK).body("success").build();

    }
  1. 测试
POST <function url>
Content-Type:application/json

{
 "BlobName":"test.txt",
 "Content":"test"
}

【讨论】:

    猜你喜欢
    • 2018-06-26
    • 2012-03-11
    • 2020-12-13
    • 2018-04-22
    • 1970-01-01
    • 2021-06-25
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多