之前數篇文章 ( 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+"
");
//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 :
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Insert_or_Replace_Entity": {
"inputs": {
"body": "@triggerBody()",
"host": {
"connection": {
"name": "@parameters('$connections')['azuretables']['connectionId']"
}
},
"method": "put",
"path": "/Tables/@{encodeURIComponent('table1')}/entities(PartitionKey='@{encodeURIComponent(triggerBody()['PartitionKey'])}',RowKey='@{encodeURIComponent(triggerBody()['RowKey'])}')"
},
"runAfter": {},
"type": "ApiConnection"
},
"Response": {
"inputs": {
"body": "@body('Insert_or_Replace_Entity')",
"statusCode": 200
},
"kind": "Http",
"runAfter": {
"Insert_or_Replace_Entity": [
"Succeeded"
]
},
"type": "Response"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"manual": {
"inputs": {
"schema": {
"$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"
}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {
"$connections": {
"value": {
"azuretables": {
"connectionId": "/subscriptions/ssssssss-xxxx-yyyy-zzzz-123123123123123/resourceGroups/SPINUS/providers/Microsoft.Web/connections/azuretables",
"connectionName": "azuretables",
"id": "/subscriptions/ssssssss-xxxx-yyyy-zzzz-123123123123123/providers/Microsoft.Web/locations/southeastasia/managedApis/azuretables"
}
}
}
}
}
Result:
結論,一個舒爽溫馨,整個就是不用很了解寫 code 就可以點點按按完成一個串接 Azure Storage Table 的 API 推薦給大家。