Azure Functions:事件驱动的无服务器代码
FreeGuideOnline
最新
2026-06-30
json { "bindings": [ { "name": "myBlob", "type": "blobTrigger", "direction": "in", "path": "images/{name}", "connection": "AzureWebJobsStorage" }, { "name": "outputTable", "type": "table", "direction": "out", "tableName": "imageMetadata", "connection": "AzureWebJobsStorage" } ] }
#### 步骤 3:编写函数代码
以下 C# 函数接收触发它的 blob 流,并构造一条日志记录写入表存储:
```csharp
using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
public static class ImageLogger
{
public class ImageLog
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string FileName { get; set; }
public long Size { get; set; }
public DateTime Timestamp { get; set; }
}
[FunctionName("ImageLogger")]
public static void Run(
[BlobTrigger("images/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob,
[Table("imageMetadata", Connection = "AzureWebJobsStorage")] out ImageLog outputTable,
string name,
ILogger log)
{
log.LogInformation($"处理 Blob\n 名称:{name} \n 大小:{myBlob.Length} 字节");
outputTable = new ImageLog
{
PartitionKey = "Images",
RowKey = Guid.NewGuid().ToString(),
FileName = name,
Size = myBlob.Length,
Timestamp = DateTime.UtcNow
};
}
}