hi I recently wrote a blog post about Azure storage I wanted to show how easy
it is to work with and the fact that you can debug and test it locally without
even having an Azure subscription what I'm gonna do in this video is walk
through the blog post step by step follow along exactly as it's written and
show you hands-on how to build and debug and Azure functions or a serverless
application using Azure storage and then deploy it to Azure
my name is Jeremy Likness I'm a cloud developer advocate with Microsoft let's
get started
starting with Visual Studio 2017 I'm going to select the azure functions
template this will open a dialog for me that allows me to enter a name for the
application so I'll type in the name and then leave create directory for
solution checked and click OK
now I have a dialogue that allows me to enter an initial trigger select my
framework choose my storage account and access rights we're going to make this
anonymous and use our local storage emulator this will spin up the project
the first thing I want to do is take the function 1 class and rename it to
something more useful in this case we'll call it link shortener
now that we have it renamed we can paste in the code from the article that
basically refactors the method provided to use an href instead of a
name the trigger by default just echoes the name back but we're building a link
shortener so the first test is can we echo an href that we passed in the query
string back so we've tested this code we're gonna go ahead and hit debug and
this is going to launch the function app host it's a fully functional host for
function endpoints that's cross-platform so this will run on any
machine and here it's found the endpoint for the function so I'm going to copy
that endpoint navigate to a web browser and paste that in and the first time
I'll get a message that says I should enter a URL so I'm going to go ahead and
add the href parameter and default this for the first attempt to my blog and hit
enter and it should echo back that I passed in the blog
for the link shortener functionality to work we need to add a few classes the
first class is a key that simply keeps track of a next up identifier so that we
can generate the code for the link shortener so this is a very simple class
that inherits from table entity which is what we use for table storage so that
has some partition key and row key and timestamp information and what's unique
for us is the identifier next we'll create a URL data class that holds the
long URL that we're mapping to as well as a count of times that that URL was
referenced now we'll add some bindings to our method the first binding will map
to a specific table entry and pull in that key entry for us so that we get the
next up ID the second entry is a generic way to interact with table storage
that's all that it takes to make the interaction if we don't have a key yet
we'll initialize it to a new value and insert it so that it is available for
later use
now we'll go ahead and remove the return value because we're going to change that
to the shortcode and add the logic to create the code basically there's a
mapping of the integer to alpha text then we create a new entity that has a
partition key which is the first character of the code the code itself
the long URL and account which will initialize to 0 and update as users
navigate the final steps are to increment the key and replace it in the
table then insert the new URL which is the mapping from the short code to the
long code and then we return that short code to the browser let's go ahead and
save that and jump into debug and see this working in the browser so I'm going
to basically refresh this page now that the storage code is there and as you can
see we get a short code return back to us
next I'm gonna use a feature built into Visual Studio called Cloud Explorer
that'll allow me to navigate to the local storage emulator and take a look
at what I've actually stored in my Azure storage table and this is locally on my
developer machine so I expand tables navigate to the URLs table which was
created for me automatically the first time I inserted a row and there you can
see I have the key that's incremented the ID as well as the
shortcode BNK that maps to the blog now that we have the ability to create a
short URL we need a new function to actually navigate so this function has a
route that takes the short URL as part of the route and we're going to give it
the function name go so that we can go to the short URL within the parameters
for this we've got the table so that we can access the table storage and we've
also mapped part of the route to the short URL parameter I've also added a
queue what I want to do is redirect as quickly as possible and so I'm going to
output the value of the shortcode to the queue so that I can update the count
later the code basically accesses the URL data if it has a match it adds the
shortcode to the queue and then it responds with a redirect so let's take a
look at that code in action we're going to launch debug again and this time
we've got a new set of APIs we've got "set" and "go" so I'm going to change the
URL after I add a new link this is a developer advocates link and I'll get
back a new code that's a BNL and then on the API I'm going to switch it from set
to go add the BNL to the end and hit enter and
what it should do is pick up the record and redirect me to the advocates page
now keep in mind that it did that quickly so that we did not update the
count yet instead we stored it in this queue that was automatically created for
us and here you can see the shortcode inside that queue
the next step is to add the code that'll process the queue so we paste that in and
there's basically a queue trigger that points to the queue name a table trigger
their table binding that points to the table name and it's a very simple
operation the table's accessed based on the shortcode if a result is found the
count is incremented and then that table entry is replaced we don't have to add
anything new because the queue already has an entry in it so we'll just go ahead
and debug and what you'll see after it starts running is that process counts
that's processing the queue executed because there was an entry in the queue from
before so we'll go into our Cloud Explorer and refresh the queue and we
see that the entry has been removed from the queue next we'll go to our table and
refresh the table and you can see that the developer advocates entry has been
incremented with a count of 1 because it's been accessed one time now that our
program is complete let's go ahead and publish it to Azure so I right click and
choose publish I'm gonna create a new function app I've got to give the app a
unique name and this is unique across all of Azure I give it a subscription
put it inside a resource group I can create a new one or pick an existing and
I also pick an app service plan and this indicates how that function app is going
to scale so I just picked a basic plan when I click create what it's going to
do is deploy these resources to Azure so the first step is creating the app
service plan and the function app so that it's ready to receive the code once
those are created along with a backing storage account the project is built and
then the project assets are deployed to the cloud now I can copy the endpoint to
the clipboard navigate to my web browser and perform the same thing first I'll
access the API and set it this time to my github repo that contains this code
so we get the short link BNK next thing I'll do is access the go API pass
the shortcode and confirm that it indeed redirects me to my repository will
navigate to short link and that's a wrap