如何用Azure Functions Blob Trigger(C#)读取Blob JSON并反序列化?选Stream还是CloudBlockBlob?
Azure Functions Blob Trigger: Stream vs CloudBlockBlob + JSON Deserialization in C#
Great question! Let’s break this down clearly for your scenario—whether to use Stream or CloudBlockBlob, plus how to handle JSON deserialization in C#.
Which to Choose: Stream or CloudBlockBlob?
- Stream: The best pick for simple read-only tasks like your JSON use case. It’s lightweight, directly gives you the blob’s content as a stream, and has minimal overhead. Perfect when you only need to read the data without modifying the blob itself.
- CloudBlockBlob: Use this if you need extra blob operations (e.g., updating metadata, deleting the blob, or accessing properties like last modified time). It gives you full control over the blob resource, but adds a bit more complexity than a basic
Stream.
For your goal of reading and deserializing a JSON blob, Stream is the most straightforward and efficient choice.
Step-by-Step Implementation with Stream
First, define a C# class that matches your JSON structure (swap this out for your actual schema):
public class JsonBlobData { public string Id { get; set; } public string Title { get; set; } public decimal Amount { get; set; } // Add other properties that match your JSON fields }
Then, build your Blob Trigger function. Here’s how to use Stream to read and deserialize the JSON:
using System.IO; using System.Text.Json; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; public class JsonBlobProcessor { [FunctionName("ProcessJsonBlob")] public void Run( [BlobTrigger("your-container-name/{name}", Connection = "AzureWebJobsStorage")] Stream blobStream, string name, ILogger log) { log.LogInformation($"Processing blob: {name}"); // Read the stream into a string using var reader = new StreamReader(blobStream); var jsonContent = reader.ReadToEnd(); // Deserialize into your custom class using System.Text.Json (default for .NET Core+) var deserializedData = JsonSerializer.Deserialize<JsonBlobData>(jsonContent); // Or use Newtonsoft.Json if you prefer (add the NuGet package first): // var deserializedData = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonBlobData>(jsonContent); if (deserializedData != null) { log.LogInformation($"Deserialized data: Id={deserializedData.Id}, Title={deserializedData.Title}"); // Add your business logic here (e.g., save to a database, process the data) } } }
Using CloudBlockBlob (If You Need Extra Blob Operations)
If you need to access blob properties or perform actions like updating metadata, switch to CloudBlockBlob:
using Microsoft.Azure.Storage.Blob; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; using System.Text.Json; public class JsonBlobProcessorWithCloudBlockBlob { [FunctionName("ProcessJsonBlobWithCloudBlockBlob")] public async Task Run( [BlobTrigger("your-container-name/{name}", Connection = "AzureWebJobsStorage")] CloudBlockBlob blob, string name, ILogger log) { log.LogInformation($"Processing blob: {name}, Last modified: {blob.Properties.LastModified}"); // Download the blob content as a string var jsonContent = await blob.DownloadTextAsync(); // Deserialize into your class var deserializedData = JsonSerializer.Deserialize<JsonBlobData>(jsonContent); if (deserializedData != null) { log.LogInformation($"Deserialized data: Id={deserializedData.Id}"); // Example: Update blob metadata to mark it as processed blob.Metadata["Processed"] = "Completed"; await blob.SetMetadataAsync(); } } }
Quick Tips
- Ensure you have the right NuGet packages: For .NET 6+, install
Microsoft.Azure.WebJobs.Extensions.Storagefor Blob Trigger bindings. If using Newtonsoft.Json, add theNewtonsoft.Jsonpackage. - Replace
your-container-namewith your actual blob container name, and update theJsonBlobDataclass to match your JSON structure. - Always dispose of streams properly (the
usingstatement handles this automatically in theStreamexample).
内容的提问来源于stack exchange,提问作者kumar
相关产品推荐
相关产品推荐

