- Azure Serverless Computing Cookbook
- Praveen Kumar Sreeram
- 360字
- 2021-06-10 18:55:56
How to do it...
Perform the following steps:
- Create a new Azure Function by choosing Azure Queue Storage Trigger from the templates.
Provide the following details after choosing the template:
- Name your function: Provide a meaningful name, such as CreateProfilePictures.
- Queue name: Name the Queue userprofileimagesqueue. This will be monitored by the Azure Function. Our previous recipe created a new item for each of the valid requests that came to the HTTP trigger (named RegisterUser) into the userprofileimagesqueue Queue. For each new entry of a queue message to this Queue storage, the CreateProfilePictures trigger will be executed automatically.
- Storage account connection: The connection of the storage account where the Queues are located.
- Review all the details and click on Create to create the new function.
- Navigate to the Integrate tab, click on New Output, choose Azure Blob Storage, and then click on the Select button.
- In the Azure Blob Storage output section, provide the following:
- Blob parameter name: Set it to outputBlob
- Path: Set it to userprofileimagecontainer/{rand-guid}
- Storage account connection: Choose the storage account where you would like to save the Blobs and click on the Save button:
- Click on the Save button to save all the changes.
- Replace the default code of the run.csx file of the CreateProfilePictures function with the following code. The following code grabs the URL from the Queue, creates a byte array, and then writes it to a Blob:
using System;
public static void Run(Stream outputBlob,string myQueueItem,
TraceWriter log)
{
byte[] imageData = null;
using (var wc = new System.Net.WebClient())
{
imageData = wc.DownloadData(myQueueItem);
}
outputBlob.WriteAsync(imageData,0,imageData.Length);
}
- Click on the Save button to save changes. Make sure that there are no compilation errors in the Logs window:
- Let's go back to the RegisterUser function and test it by providing the firstname, lastname, and ProfilePicUrl fields, like we did in the Saving the profile images to Queues using Queue output bindings recipe.
- Navigate to the Azure Storage Explorer and look at the userprofileimagecontainer Blob container. You will find a new Blob:
- You can view the image in any tool (such as MS Paint or Internet Explorer).