Question:
You're part of a large digital library that digitizes and stores thousands of books. Once a book is digitized and stored in Blob Storage, a process is triggered to generate metadata for the digitized book. After the metadata is generated, an event should be fired to notify other services for further processing (e.g., updating the search index, notifying users who requested this book). How would you set up the Azure function to handle this situation?
[FunctionName("ProcessDigitizedBook")]
public static void Run(
/* Triggers and Bindings here */
string name,
ILogger log)
{
// Process the inputBook to generate metadata
// Prepare an EventGridEvent with the metadata
// Add the event to the 'outputEvents' output binding, which will send it to Event Grid
}
Answer:
[FunctionName("ProcessDigitizedBook")]
public static void Run(
[BlobTrigger("<container-name>/{name}", Connection = "BlobStorageConnectionString")]
Stream inputBook,
[EventGrid(TopicEndpointUri = "<topic-endpoint>", TopicKeySetting = "<topic-key>")]
ICollector<EventGridEvent> outputEvents,
string name,
ILogger log)
{
// Process the inputBook to generate metadata
var metadata = GenerateMetadata(inputBook);
// Prepare an EventGridEvent with the metadata
var event = new EventGridEvent("/subject/path", "Book.Digitized", "1.0", metadata);
// Send the event
await outputEvents.AddAsync(event);
log.LogInformation($"Processed blob\n Name:{name} \n Size: {inputBook.Length} Bytes");
}