Files
spring-cloud-function/spring-cloud-function-samples/function-sample-azure-blob-trigger

== Azure Functions with Blob Trigger

IMPORTANT: For a general information about building and deploying `Azure Functions` with Spring Cloud Function, consult the https://docs.spring.io/spring-cloud-function/docs/current/reference/html/azure.html[Azure Adapter] documentation.

The Blob storage trigger starts a function when a new or updated blob is detected. The blob contents are provided as https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-input?tabs=in-process%2Cextensionv5&pivots=programming-language-java[input] to the function.

The Blob storage binding is part of an https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-register#extension-bundles[extension bundle], specified in your https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob?tabs=in-process%2Cextensionv5%2Cextensionv3&pivots=programming-language-java#install-bundle[host.json] file.


=== Usage

For local Azure Storage development you need https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azurite?tabs=visual-studio[Azurite emulator].
For the emulator you can run a docker container (see below) or use the https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azurite?tabs=visual-studio-code[Visual-Studio-Code extension].

Here is how to start the `Azure emulator` as docker container:

[source,shell]
----
docker run --name azurite --rm -p 10000:10000 -p 10001:10001 -p 10002:10002 mcr.microsoft.com/azure-storage/azurite
----

==== Package Staging folder

Use the script below to package your staging folder:

[source,shell]
----
./mvnw clean package
----

==== Run Azure Functions locally

Use the script below to run the function locally.

[source,shell]
----
./mvnw azure-functions:run
----

Use the https://azure.microsoft.com/en-us/products/storage/storage-explorer/[Azure Storage Explorer] to access the Emulator Storage Account.

Under the `Blob Containers` create 3 new containers: `test-trigger`, `test-input`, `test-output`.
Then upload the `src/test/resource/sample.txt` file into the `test-input` and the `test-trigger` folders in this order.

The appearance of the `sample.txt` file in the `test-trigger` folder triggers the `blobTest` function handler, that would look up for a file with the same name (because we used the `{name}` convention in the @BlobInput path) from the `test-input` folder.
Later is passed through the auto-wired `uppercase` service and the result is saved in the `test-output` folder.
Verify that the newly created file in `test-output` is in capitalized letters.


==== Deploy Azure Functions to Azure Cloud

Make sure you are logged in your Azure account.

[source,shell]
----
az login
----

then build and deploy

[source,shell]
----
./mvnw clean package
./mvnw azure-functions:deploy
----


==== Debug locally

Run the function in debug mode.

[source,shell]
----
./mvnw azure-functions:run -DenableDebug
----

Alternatively and the `JAVA_OPTS` value to your `local.settings.json` like this:

[source,json]
----
{
	"IsEncrypted": false,
	"Values": {
		...
		"FUNCTIONS_WORKER_RUNTIME": "java",
		"JAVA_OPTS": "-Djava.net.preferIPv4Stack=true -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=127.0.0.1:5005"
	}
}
----


For VSCode remote debug use configuration like this:

[source,json]
----
{
	"version": "0.2.0",
	"configurations": [
		{
			"type": "java",
			"name": "Attach to Remote Program",
			"request": "attach",
			"hostName": "localhost",
			"port": "5005"
		},
	    ...
	]
}
----