Saving profile picture paths to queues using queue output bindings
The previous recipe highlighted how to receive two string parameters, firstname
and lastname
, in the Request body and store them in Azure Table storage. In this recipe, let's add a new parameter named ProfilePicUrl
for the profile picture of the user that is publicly accessible via the internet. In this recipe (and the next), you'll learn about the process of extracting the URL of an image and saving it in the blob container of an Azure storage account.
While the ProfilePicUrl
input parameter can be used to download the picture from the internet, in the previous recipe, Persisting employee details using Azure Table storage output bindings, this was not feasible due to the time required to process the large size of the image, which might hinder the performance of the overall application. For this reason, it is faster to grab the URL of the profile picture and store it in a queue, which can be processed later before storing it in the blob.
Getting ready
We'll be updating the code of the RegisterUser
function that was used in the previous recipes.
How to do it…
Perform the following steps:
- Navigate to the Integrate tab of the
RegisterUser
HTTP trigger function. - Click on the New Output button, select Azure Queue Storage, and then click on the Select button.
- Provide the following parameters in the Azure Queue Storage output settings:
Message parameter name: Set the name of the parameter to
objUserProfileQueueItem
, which will be used in theRun
method.Queue name: Set the queue name to
userprofileimagesqueue
.Storage account connection: It is important to select the right storage account in the Storage account connection field.
- Click on Save to create the new output binding.
- Navigate back to the code editor by clicking on the function name (
RegisterUser
in this example) or therun.csx
file and make the changes shown in the following code:public static async Task<IActionResult> Run( HttpRequest req, CloudTable objUserProfileTable, IAsyncCollector<string> public static async Task<IActionResult> Run( Â Â Â Â HttpRequest req, Â Â Â Â CloudTable objUserProfileTable, Â Â Â Â IAsyncCollector<string> objUserProfileQueueItem, Â Â Â Â ILogger log) Â Â Â Â {.... Â Â Â Â string firstname= inputJson.firstname; Â Â Â Â string profilePicUrl = inputJson.ProfilePicUrl; Â Â Â Â await objUserProfileQueueItem.AddAsync(profilePicUrl); Â Â Â Â .... Â Â Â Â objUserProfileTable.Execute(objTblOperationInsert); Â Â Â Â }
- In the preceding code, you have added queue output bindings by adding the
IAsyncCollecter
parameter to theRun
method and just passing the required message to theAddAsync
method. The output bindings will take care of savingProfilePicUrl
to the queue. Now, click on Save to save the code changes in the code editor of therun.csx
file. - Let's test the code by adding another parameter,
ProfilePicUrl
, to the Request body and then clicking on the Run button in the Test tab of the Azure Functions code editor window. Replace"URL here"
with the URL of an image that's accessible over the internet; you'll need to make sure that the image URL provided is valid:{ "firstname": "Bill", "lastname": "Gates", "ProfilePicUrl":"URL here" }
- If everything goes fine, you'll see the Status: 200 OK message again. Then, the image URL that was passed as an input parameter in to the Request body will be created as a queue message in the Azure Queue storage service. Let's navigate to Azure Storage Explorer and view the queue named
userprofileimagesqueue
, which is the queue name that was provided in step 3. - Figure 1.15 represents the queue message that was created:
Figure 1.15: Viewing the output in Storage Explorer
How it works…
In this recipe, we added a queue message output binding and made the following changes to our existing code:
- We added a new parameter named
out string objUserProfileQueueItem
, which binds the URL of the profile picture as queue message content. - We used the
AddAsync
method ofIAsyncCollector
in theRun
method that saves the profile URL to the queue as a queue message.
In this recipe, you learned how to receive a URL of an image and save it in the blob container of an Azure storage account. In the next recipe, we'll store an image in Azure Blob Storage.