[Azure] Azure Logic App - 寫入 Azure Storage Table 資料

2021-01-19

之前數篇文章 ( https://blog.no2don.com/search/label/AzureStorage ) 我們聊到如何使用 Microsoft.Azure.Cosmos.Table  操控 Azure Storage Table ,今天我們要來看一下如何使用 Auzre Logic App 寫入 Azure Storage Table  ,這邊只能說非常溫馨…

1. 這邊是我測試的 模型資料

public class User { public string Name { get; set; } public Int32? Age { get; set; } public DateTime Create { get; set; } public string Meta { get; set; } public DateTime? LastLoginDate { get; set; } public string PartitionKey { get; set; } public string RowKey { get; set; } public string ETag { get; set; } public User(string partitionKey, string rowKey) { PartitionKey = partitionKey; RowKey = rowKey; } }

2.接下來就是進入 Azure Logic App ,並且加入一個 When a HTTP Request is Received

這時候要輸入一個 JSON Schema ,您可以到這網站轉換 https://jsonschema.net/home ,這邊是我將 Step1 的 物件轉 schema 的 code ,並且貼入

{ "$id": "http://example.com/example.json", "$schema": "http://json-schema.org/draft-07/schema", "additionalProperties": true, "default": {}, "description": "The root schema comprises the entire JSON document.", "examples": [ { "Age": 1, "Create": "2001-01-02T00:00:00", "ETag": null, "LastLoginDate": "2021-01-15T12:17:15.6441337+08:00", "Meta": "HELLO LOGIC APP", "Name": "DONMA-LOGICAPP", "PartitionKey": "CLASSA", "RowKey": "LOGICAPPKEY1" } ], "properties": { "Age": { "$id": "#/properties/Age", "default": 0, "description": "An explanation about the purpose of this instance.", "examples": [ 1 ], "title": "The Age schema", "type": "integer" }, "Create": { "$id": "#/properties/Create", "default": "", "description": "An explanation about the purpose of this instance.", "examples": [ "2001-01-02T00:00:00" ], "title": "The Create schema", "type": "string" }, "ETag": { "$id": "#/properties/ETag", "default": null, "description": "An explanation about the purpose of this instance.", "examples": [ null ], "title": "The ETag schema", "type": "null" }, "LastLoginDate": { "$id": "#/properties/LastLoginDate", "default": "", "description": "An explanation about the purpose of this instance.", "examples": [ "2021-01-15T12:17:15.6441337+08:00" ], "title": "The LastLoginDate schema", "type": "string" }, "Meta": { "$id": "#/properties/Meta", "default": "", "description": "An explanation about the purpose of this instance.", "examples": [ "HELLO LOGIC APP" ], "title": "The Meta schema", "type": "string" }, "Name": { "$id": "#/properties/Name", "default": "", "description": "An explanation about the purpose of this instance.", "examples": [ "DONMA-LOGICAPP" ], "title": "The Name schema", "type": "string" }, "PartitionKey": { "$id": "#/properties/PartitionKey", "default": "", "description": "An explanation about the purpose of this instance.", "examples": [ "CLASSA" ], "title": "The PartitionKey schema", "type": "string" }, "RowKey": { "$id": "#/properties/RowKey", "default": "", "description": "An explanation about the purpose of this instance.", "examples": [ "LOGICAPPKEY1" ], "title": "The RowKey schema", "type": "string" } }, "required": [ "Name", "Age", "Create", "Meta", "LastLoginDate", "PartitionKey", "RowKey", "ETag" ], "title": "The root schema", "type": "object" }

3.加入一個 Azure Table 的 Action,並且我是選用 InsertOrReplace ,並且將 指向 Step2 的 PK,RK  , Entity 的部分我就是選擇 The root of schema

4. 加入一個 Response ,選擇 Step3 的 body

接下來就是測試的部分了,下面是我 post 過去的 C#  code ,至於 你要 post 到哪一個網址,儲存該設計後會再 Step2 拿到網址

var sampleObject = new User("CLASSA", "LOGICAPPKEY1") { Name = "DONMA-LOGICAPP", Age = 1, Meta = "HELLO LOGIC APP", Create = DateTime.Now, LastLoginDate = DateTime.Now }; var url = "https://prod-08.southeastasia.logic.azure.com:443/workflows/bda80702ce7e4473ab123493182cdfd3/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=eHzaGUWk9L-Y8cgNp27olcwjLmagW4Smsmn-91LrJwU"; var client = new RestClient(); var request = new RestRequest(url, Method.POST); string jsonToSend =JsonConvert.SerializeObject(sampleObject); Response.Write(jsonToSend+"<hr>"); //Important request.AddParameter("application/json; charset=utf-8", jsonToSend, ParameterType.RequestBody); request.RequestFormat = DataFormat.Json; try { var c = client.ExecuteAsPost(request, "POST"); Response.Write(c.Content); } catch (Exception error) { Response.Write(error.Message); }

Azure Logic App Code :

{ &quot;definition&quot;: { &quot;$schema&quot;: &quot;https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#&quot;, &quot;actions&quot;: { &quot;Insert_or_Replace_Entity&quot;: { &quot;inputs&quot;: { &quot;body&quot;: &quot;@triggerBody()&quot;, &quot;host&quot;: { &quot;connection&quot;: { &quot;name&quot;: &quot;@parameters(&#39;$connections&#39;)[&#39;azuretables&#39;][&#39;connectionId&#39;]&quot; } }, &quot;method&quot;: &quot;put&quot;, &quot;path&quot;: &quot;/Tables/@{encodeURIComponent(&#39;table1&#39;)}/entities(PartitionKey=&#39;@{encodeURIComponent(triggerBody()[&#39;PartitionKey&#39;])}&#39;,RowKey=&#39;@{encodeURIComponent(triggerBody()[&#39;RowKey&#39;])}&#39;)&quot; }, &quot;runAfter&quot;: {}, &quot;type&quot;: &quot;ApiConnection&quot; }, &quot;Response&quot;: { &quot;inputs&quot;: { &quot;body&quot;: &quot;@body(&#39;Insert_or_Replace_Entity&#39;)&quot;, &quot;statusCode&quot;: 200 }, &quot;kind&quot;: &quot;Http&quot;, &quot;runAfter&quot;: { &quot;Insert_or_Replace_Entity&quot;: [ &quot;Succeeded&quot; ] }, &quot;type&quot;: &quot;Response&quot; } }, &quot;contentVersion&quot;: &quot;1.0.0.0&quot;, &quot;outputs&quot;: {}, &quot;parameters&quot;: { &quot;$connections&quot;: { &quot;defaultValue&quot;: {}, &quot;type&quot;: &quot;Object&quot; } }, &quot;triggers&quot;: { &quot;manual&quot;: { &quot;inputs&quot;: { &quot;schema&quot;: { &quot;$id&quot;: &quot;http://example.com/example.json&quot;, &quot;$schema&quot;: &quot;http://json-schema.org/draft-07/schema&quot;, &quot;additionalProperties&quot;: true, &quot;default&quot;: {}, &quot;description&quot;: &quot;The root schema comprises the entire JSON document.&quot;, &quot;examples&quot;: [ { &quot;Age&quot;: 1, &quot;Create&quot;: &quot;2001-01-02T00:00:00&quot;, &quot;ETag&quot;: null, &quot;LastLoginDate&quot;: &quot;2021-01-15T12:17:15.6441337+08:00&quot;, &quot;Meta&quot;: &quot;HELLO LOGIC APP&quot;, &quot;Name&quot;: &quot;DONMA-LOGICAPP&quot;, &quot;PartitionKey&quot;: &quot;CLASSA&quot;, &quot;RowKey&quot;: &quot;LOGICAPPKEY1&quot; } ], &quot;properties&quot;: { &quot;Age&quot;: { &quot;$id&quot;: &quot;#/properties/Age&quot;, &quot;default&quot;: 0, &quot;description&quot;: &quot;An explanation about the purpose of this instance.&quot;, &quot;examples&quot;: [ 1 ], &quot;title&quot;: &quot;The Age schema&quot;, &quot;type&quot;: &quot;integer&quot; }, &quot;Create&quot;: { &quot;$id&quot;: &quot;#/properties/Create&quot;, &quot;default&quot;: &quot;&quot;, &quot;description&quot;: &quot;An explanation about the purpose of this instance.&quot;, &quot;examples&quot;: [ &quot;2001-01-02T00:00:00&quot; ], &quot;title&quot;: &quot;The Create schema&quot;, &quot;type&quot;: &quot;string&quot; }, &quot;ETag&quot;: { &quot;$id&quot;: &quot;#/properties/ETag&quot;, &quot;default&quot;: null, &quot;description&quot;: &quot;An explanation about the purpose of this instance.&quot;, &quot;examples&quot;: [ null ], &quot;title&quot;: &quot;The ETag schema&quot;, &quot;type&quot;: &quot;null&quot; }, &quot;LastLoginDate&quot;: { &quot;$id&quot;: &quot;#/properties/LastLoginDate&quot;, &quot;default&quot;: &quot;&quot;, &quot;description&quot;: &quot;An explanation about the purpose of this instance.&quot;, &quot;examples&quot;: [ &quot;2021-01-15T12:17:15.6441337+08:00&quot; ], &quot;title&quot;: &quot;The LastLoginDate schema&quot;, &quot;type&quot;: &quot;string&quot; }, &quot;Meta&quot;: { &quot;$id&quot;: &quot;#/properties/Meta&quot;, &quot;default&quot;: &quot;&quot;, &quot;description&quot;: &quot;An explanation about the purpose of this instance.&quot;, &quot;examples&quot;: [ &quot;HELLO LOGIC APP&quot; ], &quot;title&quot;: &quot;The Meta schema&quot;, &quot;type&quot;: &quot;string&quot; }, &quot;Name&quot;: { &quot;$id&quot;: &quot;#/properties/Name&quot;, &quot;default&quot;: &quot;&quot;, &quot;description&quot;: &quot;An explanation about the purpose of this instance.&quot;, &quot;examples&quot;: [ &quot;DONMA-LOGICAPP&quot; ], &quot;title&quot;: &quot;The Name schema&quot;, &quot;type&quot;: &quot;string&quot; }, &quot;PartitionKey&quot;: { &quot;$id&quot;: &quot;#/properties/PartitionKey&quot;, &quot;default&quot;: &quot;&quot;, &quot;description&quot;: &quot;An explanation about the purpose of this instance.&quot;, &quot;examples&quot;: [ &quot;CLASSA&quot; ], &quot;title&quot;: &quot;The PartitionKey schema&quot;, &quot;type&quot;: &quot;string&quot; }, &quot;RowKey&quot;: { &quot;$id&quot;: &quot;#/properties/RowKey&quot;, &quot;default&quot;: &quot;&quot;, &quot;description&quot;: &quot;An explanation about the purpose of this instance.&quot;, &quot;examples&quot;: [ &quot;LOGICAPPKEY1&quot; ], &quot;title&quot;: &quot;The RowKey schema&quot;, &quot;type&quot;: &quot;string&quot; } }, &quot;required&quot;: [ &quot;Name&quot;, &quot;Age&quot;, &quot;Create&quot;, &quot;Meta&quot;, &quot;LastLoginDate&quot;, &quot;PartitionKey&quot;, &quot;RowKey&quot;, &quot;ETag&quot; ], &quot;title&quot;: &quot;The root schema&quot;, &quot;type&quot;: &quot;object&quot; } }, &quot;kind&quot;: &quot;Http&quot;, &quot;type&quot;: &quot;Request&quot; } } }, &quot;parameters&quot;: { &quot;$connections&quot;: { &quot;value&quot;: { &quot;azuretables&quot;: { &quot;connectionId&quot;: &quot;/subscriptions/ssssssss-xxxx-yyyy-zzzz-123123123123123/resourceGroups/SPINUS/providers/Microsoft.Web/connections/azuretables&quot;, &quot;connectionName&quot;: &quot;azuretables&quot;, &quot;id&quot;: &quot;/subscriptions/ssssssss-xxxx-yyyy-zzzz-123123123123123/providers/Microsoft.Web/locations/southeastasia/managedApis/azuretables&quot; } } } } }

Result:

結論,一個舒爽溫馨,整個就是不用很了解寫 code 就可以點點按按完成一個串接 Azure Storage Table 的 API 推薦給大家。


當麻許的碎念筆記 2014 | Donma Hsu Design.