azure-docs icon indicating copy to clipboard operation
azure-docs copied to clipboard

Java BlobOutput path substitution based on HttpTrigger body

Open Lesrac opened this issue 2 years ago • 2 comments

Is there a way to modify the "path" for the @BlobOutput annotation inside the code? Use Case: Receiving an XML through an HttpTrigger. Reading stuff out of the received XML would determine how the path inside the BlobOutput should look like, e.g. some base "myblob/" is defined and on top of it we read out of the xml the "senderId" and a "filename". As we do some checks on the XML, we determine that the received body is more "trustworthy" than possible sent headers (and/or the sending party can't be modified to send the needed headers).

Code example:

@Suppress("TooGenericExceptionCaught")
@FunctionName("myRequest")
@StorageAccount("AzureWebJobsBlobSpace")
fun execute(
    @HttpTrigger(
        name = "request",
        methods = [HttpMethod.POST],
        authLevel = AuthorizationLevel.ANONYMOUS
    ) request: HttpRequestMessage<String>, context: ExecutionContext,
    @BlobOutput(
        name = "target",
        path = "myblob/"
    ) outputItem: OutputBinding<String>
): HttpResponseMessage {
    logger.info { "BEGIN request for participant_number '${request.headers["participant_number"] ?: "none"}'" }
    return try {
        val handleRequest = super.handleRequest(request.body, context)
        // use response from handleRequest to determine the blob path
        outputItem.value = request.body
        request
            .createResponseBuilder(HttpStatus.ACCEPTED)
            .body("done")
            .build()
    } catch (e: Exception) {
        handleError(e, request, context)
    }
}

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Lesrac avatar Nov 07 '22 10:11 Lesrac

@Lesrac Thanks for your feedback! We will investigate and update as appropriate.

SaibabaBalapur-MSFT avatar Nov 07 '22 13:11 SaibabaBalapur-MSFT

@Lesrac , The "path" for the @BlobOutput cannot be changed at runtime as it is a part of the Function definition. You will have to use the blob storage java SDK to write to the blob once you figure out the path

@Lesrac , Hope my answer helps! I will close this issue for now. Feel free to reopen if required