Connecting to SFTP server using azure logic apps

Lot of project requires connecting to external SFTP file location for uploading files .This is usually used for integrating two systems in an offline manner where bulk information from one system is passed on to another system in the form of file. Or it could just be some reports or files delivered to some third party.

Usually this kind of stuff is accomplished by using SFTP client libraries e.g. SSH.Net for .Net based applications. This approach works well but is not that scalable.Alternative to this kind of thinking is leveraging serverless functionality such as Azure’s logic apps. Logic apps are very powerful and can be used to orchestrate various different kind of business flows. Below I’ll show you are very simple flow where we can upload files to SFTP without using a client library and by writing bare minimum code.

In this sample application we would upload the file to an azure blob which will trigger the logic app workflow and take the file and upload it to sftp location and once done indicate back that upload is completed.

Create a storage account

Create a new storage account (if you don’t already have it) for uploading the file to a blob container which will trigger the workflow.

Create a logic app

Go to azure portal and create a new logic app.

ScreenClip

Logic app works with triggers and actions. For accomplishing this task we will do use two triggers and one action.

Create a trigger for when blob is added or modified

ScreenClip

This is triggered if a file is uploaded in “tempcontainer” of the blob.

Add a trigger to get blob content

ScreenClip

Add a new trigger which will get the blob content from the path of the container.

Add action for creating file

Last part is to add action for creating file at sftp location from the content of the blob.

  • For this choose SFTP connector and select action “Create file”

ScreenClip

  • Create a new sftp connection

ScreenClip

  • Specify the target folder path and choose the file name and the content from dynamic properties.

ScreenClip

Upload file to blob for triggering the workflow

Next step is to have a mechanism for uploading file to blob. This could be anything ,in my case I just used below code snippet from a .net based console app.

static void UploadFileStreamToBlob()
        {
            Console.WriteLine("Starting - UploadFileStreamToBlob");
            var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            var myClient = storageAccount.CreateCloudBlobClient();
            var container = myClient.GetContainerReference(containerName);
            container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
            var blockBlob = container.GetBlockBlobReference("Test_" + DateTime.UtcNow.Ticks);
            var path = "Test_" + DateTime.UtcNow.Ticks + ".txt";
            Console.WriteLine("Starting file creation");
            using (var fileStream = File.OpenWrite(path))
            {
                var writer = new StreamWriter(fileStream);
                for (int i = 0; i < noOfLines; i++)
                {
                    writer.WriteLine($"####################################[Line - {i}]#########################################");
                }
            }
            
            Console.WriteLine($"file creation completed");
            using (var fileStream = File.OpenRead(path))
            {
                blockBlob.UploadFromStream(fileStream);
            }
            Console.WriteLine("Upload complete.");
        }

To further enhance the solution we can also add 4th step where once the file is created / uploaded at sftp location there would be call back of sorts indicating the result. We can use a queue  message for this purpose or just an http endpoint so that once done the logic apps will call our api with the result.

This way we can also make alternate flows like what happens if an error occurs while uploading, building complex retry workflows etc without even writing a single line of code.

If you are interested in knowing more about logic apps refer this pluralsight course which gives you an excellent overview of how logic apps work.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.