Check whether a file exists using the File Storage connector in Logic Apps

Published on 18 July 2018 by Perry. Reading time: 3 minutes.

Logic Apps is an Azure component that allows you to implement powerful, serverless, and scalable automated workflows. It allows data exchange with other Azure components and also on-premise data. You can develop Logic Apps using an easy to use visual designer. In a Logic App the data exchange takes place through so called connectors of which there are approximately 200+ connectors currently available and the list is still growing rapidly.

Problem

For a scheduled interface I developed in Logic Apps, I needed to check whether a file exists in Azure File Storage and then import the file content. This sounds simple, but it wasn’t because although many Actions are available for the Azure File Storage connector, checking whether a file exists isn’t one of them.

Perry-Logic-Apps-1

For this blog, I have created a File Share in my Azure File Storage called “dropfolder”. In this File Share the check is if a file called “input.json” is there.

Perry-Logic-Apps-2

One of the first thing I then tried is implementing the Actions that can be used for retrieving the file, for example “Get file content” and “Get file metadata”.

Perry-Logic-Apps-3

Unfortunately, none of these Actions can be used because an error will occur when the file is not available and the workflow will not continue. Although it is correct that the workflow cannot continue, it also prevents any further actions like sending notifications. Because this is not an actual error, I knew the file may not be available, it may raise some unjustified red flags.

Perry-Logic-Apps-4

Workaround

The workaround for this problem is based on the “List files” Action. This Action returns a list of files which reside in a given File Share. When there are no files in the File Share, the output is empty and (most import) no errors are raised.

Perry-Logic-Apps-5

Next step, is to filter the outcome of the “List files” Action. For this, the Action called “Filter array” is used. The input of the filter is output of “List files” and the filter consists of the “equals” comparison.

Perry-Logic-Apps-6

If the file exists, the output of the “Filter array” is a JSON array. If the file does not exist, the output will be empty. Last step is therefore to check the “Filter array” using a “Condition”.

Perry-Logic-Apps-7

Conclusion

With the checks in place we can run the Logic App without any file present. As you can see in the image the false condition is run (see the green check) and not the true condition (a black cross).

Perry-Logic-Apps-8

After this I’ve placed the file in my File Share and ran the Logic App again, and now the true condition is hit.

Perry-Logic-Apps-9

That’s it; check whether a file exists in 3 simple steps.

 

Tags: Azure

Perry

Published by Perry on 18 July 2018

In short; developer in his mid-forties, living in Alphen aan den Rijn (The Netherlands), married with Carola and has two awesome kids named Martijn and Jeroen. At young age I was fascinated by the possibilities of my first computer; a Commodore 16 with no less than 16 KB RAM. After getting my degree in electrical engineering I first started programming PLC’s for process automation. After a couple of years I gradually entered the world of Microsoft software development starting with Visual Basic 3.0 and Visual InterDev 6.0 as development environment. In recent years I have focussed on Azure technologies. I like to experiment with new technologies and solve complex issues with simple solutions.

 

Comments? Share them below

Learn more about ETTU

And the way we work