Код: Выделить всё
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Azure.Storage.Blobs;
using System.Text.Json;
using CsvHelper;
using System.Globalization;
using Azure.Messaging.EventGrid;
public class BlobTriggerFunction
{
private readonly ILogger _logger;
private readonly BlobServiceClient _blobServiceClient;
public BlobTriggerFunction(ILoggerFactory loggerFactory, BlobServiceClient blobServiceClient)
{
_logger = loggerFactory.CreateLogger();
_blobServiceClient = blobServiceClient;
}
[Function("BlobTriggerFunction")]
public async Task Run([EventGridTrigger] EventGridEvent eventGridEvent)
{
_logger.LogInformation($"Event Grid trigger function processed an event: {eventGridEvent.EventType}");
if (eventGridEvent.EventType != "Microsoft.Storage.BlobCreated")
{
_logger.LogInformation($"Ignoring event type: {eventGridEvent.EventType}");
return;
}
var blobCreatedData = eventGridEvent.Data.ToObjectFromJson();
var sourceContainerName = blobCreatedData.Url.Split('/')[3];
var sourceBlobName = blobCreatedData.Url.Split('/')[4];
var destinationContainerName = "secondary";
var destinationBlobName = Path.ChangeExtension(sourceBlobName, ".csv");
var sourceBlobClient = _blobServiceClient.GetBlobContainerClient(sourceContainerName).GetBlobClient(sourceBlobName);
var destinationBlobClient = _blobServiceClient.GetBlobContainerClient(destinationContainerName).GetBlobClient(destinationBlobName);
// Download the source blob content
using var sourceStream = await sourceBlobClient.OpenReadAsync();
using var streamReader = new StreamReader(sourceStream);
var jsonContent = await streamReader.ReadToEndAsync();
// Parse JSON and convert to CSV
var jsonArray = JsonSerializer.Deserialize(jsonContent);
using var memoryStream = new MemoryStream();
using var writer = new StreamWriter(memoryStream);
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
if (jsonArray.Any())
{
var headers = jsonArray[0].Keys.ToArray();
foreach (var header in headers)
{
csv.WriteField(header);
}
csv.NextRecord();
foreach (var item in jsonArray)
{
foreach (var header in headers)
{
if (item.TryGetValue(header, out JsonElement value))
{
csv.WriteField(value.ToString());
}
else
{
csv.WriteField(string.Empty);
}
}
csv.NextRecord();
}
}
writer.Flush();
memoryStream.Position = 0;
// Upload the CSV to the destination container
await destinationBlobClient.UploadAsync(memoryStream, true);
_logger.LogInformation($"CSV file created and uploaded: {destinationBlobName}");
}
}
public class BlobCreatedEventData
{
public string Api { get; set; }
public string ClientRequestId { get; set; }
public string RequestId { get; set; }
public string ETag { get; set; }
public string ContentType { get; set; }
public int ContentLength { get; set; }
public string BlobType { get; set; }
public string Url { get; set; }
public string Sequencer { get; set; }
public StorageDiagnostics StorageDiagnostics { get; set; }
}
public class StorageDiagnostics
{
public string BatchId { get; set; }
}

Мне не удалось больше ничего попробовать, так как я новичок в этом, и большая часть информации в онлайн-руководствах, похоже, предназначена для более старых версий как пользовательского интерфейса портала Azure, так и, возможно, более старых версий приложения функций Azure (может быть, v3?)
Извините за неопределенность этого поста, так как мне в основном сказали: «Вы знаете C #, сделайте это!» независимо от того, работал ли я раньше с сеткой событий или нет.
Подробнее здесь: https://stackoverflow.com/questions/789 ... bscription
Мобильная версия