>> Hi everyone. I'm Marie.
I'm a Software Engineer on the Azure Functions Team.
In this video, I'm going to be explaining triggers and bindings.
Let's start with some definitions.
Triggers are what cause a function to run.
Every serverless function needs
a trigger because serverless functions are event-driven.
They only run in response to an event.
A trigger defines that event that causes the code to run.
Some common examples of triggers are HTTP triggers,
timer triggers, and queue triggers.
Bindings offer a declarative way
of connecting other resources to functions.
Bindings connect to resources
before and after a function executes.
An input binding, which resolves before the function executes,
brings in additional data for the function to work with.
An output binding which resolves after the function executes,
pushes data from the function out to another resource.
You don't need an input or an output binding in your function,
but they can't be useful.
Let's see what this actually looks like with a code example.
In this demo, I'll be using an HTTP trigger,
a blob input binding,
and a blob output binding.
Here's my function app,
which is a collection of functions that deploy and scale together.
I use Azure Functions extension for VS Code,
which gives me the ideal development environment.
This is my single function called upload profile image.
Function.json contains the definitions
for my triggers and other bindings.
Here I have an HTTP trigger,
which I know from the type,
as well as the direction.
My next binding is a HTTP output,
which corresponds to the HTTP response.
Lastly, I have a blob output binding.
Each binding definition contains a type, direction,
and name, where name is
the property that's going to get passed in to my function.
The other properties here are binding specific.
In this case, connection refers to the name of the key in
my local settings or app settings that
has a reference to the connection string for a storage account.
Let's press "F5" to debug.
My function will run when it's
triggered by an HTTP request to this route here.
The username I enter as part of the route will then be
used here as part of the blob binding path.
This is a binding expression.
The code that runs will expect multi-part form data and it will
pass that into some sort of file name and file data.
I'm going to convert that binary data into a binary string.
Then push that to profile image,
which will end up under profiles
username.json in my Azure storage account.
Let's run this URL which I've already got set up in postmen.
Zappy here is my username so I'm going to expect in
Azure Storage some file called zappy.json.
When I refresh my Azure portal,
I see that a folder was created.
Now I see zappy.json.
If I edit it, I should see that binary string as well.
This shows what we can do with
a HTTP trigger and a output blob binding.
Now let's see what we can do with an input blob binding.
For that, I'm going to create a new function,
which is still going to be HTTP trigger.
This time I'm going to get profile image.
I need to add a blob input bindings.
So I'm going to right-click and then add binding.
This is going to be an input binding.
It's going to be for Azure blob storage.
This one is also going to be called profile image.
The path is going to be profiles/username.
I'm going to configure
that route parameter in the same way that my first function did.
So now this is configured to just have a plain old route,
but I want a custom route with
username as part of that route data.
I'm going to copy this similar format.
I'm going to add it to my function.json
HTTP trigger binding definition
and change it a little because it's not uploading anymore.
Now in my function code,
I need to do something about this profile image.
Like I mentioned, an input binding brings data into the function.
It is either passed in here as an argument or it can be
accessed on context.bindings with the same name,
profile image in this case.
I'm going to be using the data we get from the arguments.
I'm going to modify this code so that
instead of this text message,
I'm going to be outputting the image from my blob account.
So I'm going to be deleting most of
this and changing what's put in the body.
I know that the body is going to come from profile
image.data because that was
the format of my json object that I put in there.
So body is going to be profile image.data.
I need to convert this data from
a string representation of binary.
So I'm going to do buffer.from profile data string type binary.
Next I also stored the type which
is the content type for the HTTP header.
Going to add a header's object with
content type set to profile image.type.
I'm going to send and expect to see that image that I uploaded
earlier has a response. There it is.
This is the profile image of our username zappy.
I only showed you two types of triggers and bindings today,
but there are so many more triggers and
bindings that are supported out of the box.
As you can see, there are
many Azure and non-Azure services that
integrate with Azure functions through triggers and bindings.
This means that there are a lot of use cases for
serverless functions beyond serverless web APIs.
Thank you so much for listening today and I'll
drop some follow-up links down below.
[MUSIC]