Search code examples
azure-logic-apps

Automating Daily File Transfer and Storage Using Azure Logic Apps for Daimler Project


I am working on automating a file transfer process using Azure Logic Apps, where a file is sent from one user to another twice daily, at 5 AM and 5 PM. The goal is to automatically store the files in specific Azure Blob Storage locations based on the time they are received. Here's what I need to achieve:

At 5 AM: Store the received file in the Azure Blob Storage container path: /rk/test. At 5 PM: Store the received file in the Azure Blob Storage container path: /rk/test/archive What I Have Done So Far: I've set up a Logic App with a Recurrence trigger to run at 5 AM and 5 PM daily. I'm using the Outlook 365 connector to fetch emails with attachments sent by a specific person. I've also added a Blob Storage connector to upload the file to the required storage location. The Challenge: I’m struggling to implement the logic to distinguish between the 5 AM and 5 PM files so that they are routed to the correct storage paths. Specifically, I'm unsure how to:

Check the current time and apply conditional logic to route the file to the correct Blob Storage path. Ensure that the Logic App runs as intended and processes files sent at these specific times without missing any or routing them incorrectly.

I've set up a Logic App with a Recurrence trigger to run at 5 AM and 5 PM daily. I'm using the Outlook 365 connector to fetch emails with attachments sent by a specific person. I've also added a Blob Storage connector to upload the file to the required storage location.


Solution

  • You need to implement Condition action to check the timings like 5 AM or 5 PM and also convert the time accordingly as in my case I am using IST time zone.

    I am using two condition action and below given workflow to get the expected response.

    enter image description here

    enter image description here

    enter image description here

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Condition": {
                    "actions": {
                        "For_each_1": {
                            "actions": {
                                "For_each": {
                                    "actions": {
                                        "Create_blob_(V2)": {
                                            "inputs": {
                                                "body": "@base64ToBinary(items('For_each')?['contentBytes'])",
                                                "headers": {
                                                    "ReadFileMetadataFromServer": true
                                                },
                                                "host": {
                                                    "connection": {
                                                        "name": "@parameters('$connections')['azureblob']['connectionId']"
                                                    }
                                                },
                                                "method": "post",
                                                "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files",
                                                "queries": {
                                                    "folderPath": "/sample-container/rk/test/",
                                                    "name": "@items('For_each')?['name']",
                                                    "queryParametersSingleEncoded": true
                                                }
                                            },
                                            "runtimeConfiguration": {
                                                "contentTransfer": {
                                                    "transferMode": "Chunked"
                                                }
                                            },
                                            "type": "ApiConnection"
                                        }
                                    },
                                    "foreach": "@item()?['attachments']",
                                    "type": "Foreach"
                                }
                            },
                            "foreach": "@body('Get_emails_(V3)')?['value']",
                            "type": "Foreach"
                        }
                    },
                    "else": {
                        "actions": {
                            "Condition_1": {
                                "actions": {
                                    "For_each_3": {
                                        "actions": {
                                            "For_each_2": {
                                                "actions": {
                                                    "Create_blob_(V2)_1": {
                                                        "inputs": {
                                                            "body": "@base64ToBinary(items('For_each_2')?['contentBytes'])",
                                                            "headers": {
                                                                "ReadFileMetadataFromServer": true
                                                            },
                                                            "host": {
                                                                "connection": {
                                                                    "name": "@parameters('$connections')['azureblob']['connectionId']"
                                                                }
                                                            },
                                                            "method": "post",
                                                            "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files",
                                                            "queries": {
                                                                "folderPath": "/sample-container/rk/test/archive",
                                                                "name": "@items('For_each_2')?['name']",
                                                                "queryParametersSingleEncoded": true
                                                            }
                                                        },
                                                        "runtimeConfiguration": {
                                                            "contentTransfer": {
                                                                "transferMode": "Chunked"
                                                            }
                                                        },
                                                        "type": "ApiConnection"
                                                    }
                                                },
                                                "foreach": "@item()?['attachments']",
                                                "type": "Foreach"
                                            }
                                        },
                                        "foreach": "@body('Get_emails_(V3)')?['value']",
                                        "type": "Foreach"
                                    }
                                },
                                "else": {
                                    "actions": {}
                                },
                                "expression": {
                                    "and": [
                                        {
                                            "equals": [
                                                "@int(formatDateTime(convertTimeZone(utcNow(), 'UTC', 'India Standard Time'), 'HH'))",
                                                17
                                            ]
                                        }
                                    ]
                                },
                                "type": "If"
                            }
                        }
                    },
                    "expression": {
                        "and": [
                            {
                                "equals": [
                                    "@int(formatDateTime(convertTimeZone(utcNow(), 'UTC', 'India Standard Time'), 'HH'))",
                                    5
                                ]
                            }
                        ]
                    },
                    "runAfter": {
                        "Get_emails_(V3)": [
                            "Succeeded"
                        ]
                    },
                    "type": "If"
                },
                "Get_emails_(V3)": {
                    "inputs": {
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['office365']['connectionId']"
                            }
                        },
                        "method": "get",
                        "path": "/v3/Mail",
                        "queries": {
                            "fetchOnlyFlagged": false,
                            "fetchOnlyUnread": true,
                            "fetchOnlyWithAttachment": true,
                            "folderPath": "Inbox",
                            "importance": "Any",
                            "includeAttachments": true,
                            "top": 10
                        }
                    },
                    "runAfter": {},
                    "type": "ApiConnection"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {
                "$connections": {
                    "defaultValue": {},
                    "type": "Object"
                }
            },
            "triggers": {
                "Recurrence": {
                    "evaluatedRecurrence": {
                        "frequency": "Day",
                        "interval": 1,
                        "schedule": {
                            "hours": [
                                "5",
                                "17"
                            ],
                            "minutes": [
                                0
                            ]
                        },
                        "timeZone": "India Standard Time"
                    },
                    "recurrence": {
                        "frequency": "Day",
                        "interval": 1,
                        "schedule": {
                            "hours": [
                                "5",
                                "17"
                            ],
                            "minutes": [
                                0
                            ]
                        },
                        "timeZone": "India Standard Time"
                    },
                    "type": "Recurrence"
                }
            }
        },
        "parameters": {
            "$connections": {
                "value": {
                    "azureblob": {
                        "connectionId": "/subscriptions/0e8*******5e7c/resourceGroups/*********/providers/Microsoft.Web/connections/azureblob",
                        "connectionName": "azureblob",
                        "id": "/subscriptions/0e8********5e7c/providers/Microsoft.Web/locations/eastus/managedApis/azureblob"
                    },
                    "office365": {
                        "connectionId": "/subscriptions/0e8*********5e7c/resourceGroups/*******/providers/Microsoft.Web/connections/office365",
                        "connectionName": "office365",
                        "id": "/subscriptions/0e8*******5e7c/providers/Microsoft.Web/locations/eastus/managedApis/office365"
                    }
                }
            }
        }
    }