Created
August 27, 2021 07:50
-
-
Save magicsih/5e8ba911cabf198ec28490ae25214cde to your computer and use it in GitHub Desktop.
AzureFunction EventGrid, BlobStorageEvent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Default URL for triggering event grid function in the local environment. | |
// http://localhost:7071/runtime/webhooks/EventGrid?functionName={functionname} | |
using System; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Host; | |
using Microsoft.Azure.EventGrid.Models; | |
using Microsoft.Azure.WebJobs.Extensions.EventGrid; | |
using Microsoft.Extensions.Logging; | |
using Newtonsoft.Json.Linq; | |
using System.IO; | |
using Azure.Storage.Blobs; | |
using System.Data; | |
using System.Text; | |
using System.Data.SqlClient; | |
namespace AzureFunctionApp | |
{ | |
public static class Functions | |
{ | |
private static readonly string BLOB_STORAGE_CONNECTION_STRING = Environment.GetEnvironmentVariable("AzureWebJobsStorage"); | |
private static readonly string SQLDB_CONNECTION_STRING = Environment.GetEnvironmentVariable("sqldb_connection"); | |
[FunctionName("ReadXmlAndInsertIntoDB")] | |
public static void Run([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log) | |
{ | |
log.LogInformation(eventGridEvent.Data.ToString()); | |
var createdEvent = ((JObject)eventGridEvent.Data).ToObject<StorageBlobCreatedEventData>(); | |
if(createdEvent.Api.Equals("PutBlob") || createdEvent.Api.Equals("PutBlockList")) { | |
var blobName = Path.GetFileNameWithoutExtension(GetBlobNameFromUrl(createdEvent.Url)); // {guid}_{suffix} | |
string[] blobNames = blobName.Split("_"); | |
string guid = blobNames[0]; | |
string suffix = blobNames[1]; | |
log.LogInformation("BLOB NAME:{0} GUID:{1} TYPE:{2}", blobName, guid, suffix); | |
BlobServiceClient blobServiceClient = new BlobServiceClient(BLOB_STORAGE_CONNECTION_STRING); | |
BlobContainerClient blobContainerClient; | |
BlobClient blobClient; | |
blobContainerClient = blobServiceClient.GetBlobContainerClient("schema"); | |
blobClient = blobContainerClient.GetBlobClient(suffix + ".xml"); | |
log.LogDebug("DOWNLOADING SCHEMA..."); | |
MemoryStream ms = new MemoryStream(); | |
var schemaResponse = blobClient.DownloadTo(ms); | |
ms.Position = 0; | |
DataTable table = new DataTable(); | |
table.ReadXmlSchema(ms); | |
blobContainerClient = blobServiceClient.GetBlobContainerClient("data"); | |
blobClient = blobContainerClient.GetBlobClient(guid + "_" + suffix + ".xml"); | |
log.LogDebug("DOWNLOADING DATA..."); | |
ms = new MemoryStream(); | |
var dataResponse = blobClient.DownloadTo(ms); | |
ms.Position = 0; | |
table.ReadXml(ms); | |
log.LogDebug("INSERTING INTO DB..."); | |
//log.LogInformation(DebugString(table)); | |
try | |
{ | |
using (SqlConnection conn = new SqlConnection(SQLDB_CONNECTION_STRING)) | |
{ | |
conn.Open(); | |
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(conn)) | |
{ | |
sqlBulkCopy.DestinationTableName = table.TableName; | |
foreach (var column in table.Columns) | |
{ | |
sqlBulkCopy.ColumnMappings.Add(column.ToString(), column.ToString()); | |
} | |
sqlBulkCopy.WriteToServer(table); | |
} | |
} | |
} catch(Exception ex) | |
{ | |
log.LogError(ex.Message); | |
} | |
} else | |
{ | |
log.LogInformation("{0} is ignored", createdEvent.Api); | |
} | |
} | |
private static string GetBlobNameFromUrl(string bloblUrl) | |
{ | |
var uri = new Uri(bloblUrl); | |
var blobClient = new BlobClient(uri); | |
return blobClient.Name; | |
} | |
private static string DebugString(DataTable table) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
var columns = table.Columns.GetEnumerator(); | |
sb.Append(Environment.NewLine); | |
sb.AppendLine(table.TableName); | |
sb.AppendLine("=========================================================================================="); | |
while (columns.MoveNext()) | |
{ | |
DataColumn current = (DataColumn)columns.Current; | |
sb.AppendLine(string.Format("{0,25}\t{1,30}", current.ColumnName, current.DataType.ToString())); | |
} | |
sb.AppendLine("=========================================================================================="); | |
//Console.WriteLine(string.Join(" | ", columnNames)); | |
var rows = table.Rows.GetEnumerator(); | |
while (rows.MoveNext()) | |
{ | |
DataRow row = (DataRow)rows.Current; | |
sb.AppendLine(string.Join(" | ", row.ItemArray)); | |
} | |
sb.Append(Environment.NewLine); | |
return sb.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment