question

KeshavRajput-6587 avatar image
0 Votes"
KeshavRajput-6587 asked VenkateshDodda-MSFT answered

How to get latest file from Azure file share using Azure Logic App?

Hi Team,


I am working on scenario in that I need to transfer the file from one location to another using Azure Logic App. Let's take an example : -
Suppose I have a folder name TEST in azure file share and inside folder has multiple files. My Aim is to copy only latest file from TEST folder to TEST-1 folder.


Please provide me the steps to perform my task.

Thanks

azure-logic-apps
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

VenkateshDodda-MSFT avatar image
0 Votes"
VenkateshDodda-MSFT answered

@keshavRajput-6587 Thanks for reaching out. Unfortunately, we don't have any built-in Azure file storage connector triggers to copy files from one location to another location. To achieve this, you need to create a custom workflow by combining multiple actions (compose, list files, get file, control, copy file). I have created a workflow by including all the actions that i have mentioned before.

  1. Used recurrence as trigger to run the workflow at a frequency for every minute.

  2. Using Get Files Pulled the list of existing files in that file share.

  3. Added for each loop to iterate those lists of files using Get File metadata using path to get the file creation time.

  4. Added the below condition to check the existing file is latest or not, if the condition is true, it will copy the file from source location to destination location.

    div(sub(ticks(utcNow()),ticks(body('Get_file_metadata_using_path')?['LastModified'])),3600000000)

workflow. Json file:

 {
     "definition": {
         "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
         "actions": {
             "For_each": {
                 "actions": {
                     "Condition": {
                         "actions": {
                             "Copy_file": {
                                 "inputs": {
                                     "headers": {
                                         "ReadFileMetadataFromServer": true
                                     },
                                     "host": {
                                         "connection": {
                                             "name": "@parameters('$connections')['azurefile']['connectionId']"
                                         }
                                     },
                                     "method": "post",
                                     "path": "/datasets/default/copyFile",
                                     "queries": {
                                         "destination": "<destinationPath>",
                                         "overwrite": true,
                                         "queryParametersSingleEncoded": true,
                                         "source": "@items('For_each')?['Path']"
                                     }
                                 },
                                 "runAfter": {},
                                 "type": "ApiConnection"
                             }
                         },
                         "expression": {
                             "and": [
                                 {
                                     "equals": [
                                         "@div(sub(ticks(utcNow()),ticks(body('Get_file_metadata_using_path')?['LastModified'])),3600000000)",
                                         0
                                     ]
                                 }
                             ]
                         },
                         "runAfter": {
                             "Get_file_metadata_using_path": [
                                 "Succeeded"
                             ]
                         },
                         "type": "If"
                     },
                     "Get_file_metadata_using_path": {
                         "inputs": {
                             "host": {
                                 "connection": {
                                     "name": "@parameters('$connections')['azurefile']['connectionId']"
                                 }
                             },
                             "method": "get",
                             "path": "/datasets/default/GetFileByPath",
                             "queries": {
                                 "path": "@items('For_each')?['Path']",
                                 "queryParametersSingleEncoded": true
                             }
                         },
                         "runAfter": {},
                         "type": "ApiConnection"
                     }
                 },
                 "foreach": "@body('List_files')?['value']",
                 "runAfter": {
                     "List_files": [
                         "Succeeded"
                     ]
                 },
                 "type": "Foreach"
             },
             "List_files": {
                 "inputs": {
                     "host": {
                         "connection": {
                             "name": "@parameters('$connections')['azurefile']['connectionId']"
                         }
                     },
                     "method": "get",
                     "path": "/datasets/default/foldersV2/@{encodeURIComponent(encodeURIComponent('JTJmdGVzdGluZ2ZpbGVz'))}",
                     "queries": {
                         "nextPageMarker": "",
                         "useFlatListing": false
                     }
                 },
                 "metadata": {
                     "JTJmdGVzdGluZ2ZpbGVz": "/testingfiles"
                 },
                 "runAfter": {},
                 "type": "ApiConnection"
             }
         },
         "contentVersion": "1.0.0.0",
         "outputs": {},
         "parameters": {
             "$connections": {
                 "defaultValue": {},
                 "type": "Object"
             }
         },
         "triggers": {
             "Recurrence": {
                 "evaluatedRecurrence": {
                     "frequency": "Minute",
                     "interval": 1
                 },
                 "recurrence": {
                     "frequency": "Minute",
                     "interval": 1
                 },
                 "type": "Recurrence"
             }
         }
     },
     "parameters": {
         "$connections": {
             "value": {
                 "azurefile": {
                     "connectionId": "/subscriptions/<subscriptionId>/resourceGroups/<resourcegroup>/providers/Microsoft.Web/connections/azurefile",
                     "connectionName": "azurefile",
                     "id": "/subscriptions/<subscriptionId>/providers/Microsoft.Web/locations/eastus/managedApis/azurefile"
                 }
             }
         }
     }
 }

Here is the screenshot of the Workflow Designer:

200512-image.png

Feel free to reach back to me if you have any further questions.


image.png (51.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.