Functions (1 / 54): Write an Azure function that uses HTTP trigger and takes id from the url and text from request body, then appends the text to existing blob with id id and logs its new content. Use Steam for the blob.
[FunctionName("BlobAppend")]
public static void Run(
/* Triggers and Bindings here */
ILogger log)
{
// Code here
}
Answer:
[FunctionName("BlobAppend")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
[Blob("{id}", FileAccess.ReadWrite)] Stream myBlob,
ILogger log)
{
var id = req.Query["id"];
var requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
// Read existing content from the blob
var reader = new StreamReader(myBlob);
var content = reader.ReadToEnd() + data?.text;
var contentBytes = System.Text.Encoding.UTF8.GetBytes(content);
// Write to the blob
myBlob.Write(contentBytes, 0, contentBytes.Length);
myBlob.Position = 0;
log.LogInformation($"Blob: {id}\n Content:{content} \n Size: {myBlob.Length} bytes");
}