How to Automatically Save Incoming SFTP Files to SharePoint With Azure Logic Apps
Managed SFTP services like SFTP To Go are very handy for receiving files from third parties securely, but people tend to use other services for real-time collaboration. So whatever files you get in your inbound SFTP folder often need to end up elsewhere like Dropbox or SharePoint, and it’s completely sensible to expect that to happen automagically.
Azure Logic Apps can serve as a convenient integration layer here. This low-code platform comes with a variety of connectors that make it possible to move files between numerous services.
In the previous two posts, we discussed how to interact with SFTP To Go using the SFTP-SSH connector in Azure Logic Apps. In this post, we’ll explore how to fetch new files from SFTP To Go and copy them to a predefined folder on SharePoint.
How the SFTP to SharePoint workflow works
Here is the basic logic of the app we will build.
- SFTP To Go will watch for specific files to show up in a folder and trigger a webhook.
- The Logic App will listen to incoming HTTP requests, get the file data over an SFTP/SSH connection, and save it to a folder on a SharePoint site.
What you need before you create the Logic App
- An active SFTP To Go account with permission to create credentials, folders, and automations, and upload data
- An Azure subscription and permissions to create a Logic App
- A service account (a Microsoft 365 work account) with read/write access to the relevant SharePoint site
- Representative test files
Step by step: Sending files from SFTP to SharePoint
The workflow has two components: an automation on SFTP To Go that looks for new files and triggers a webhook, and a Logic App that listens to HTTP requests, gets content from SFTP To Go, and saves it to SharePoint.
Step 1. Create a new Azure Logic App
Create a new Logic App following this section of our previous guide. Use the Consumption - Multi-tenant hosting option. If you already completed the previous step-by-step guide, use the same resource group to reuse an already established SharePoint connection.
Step 2. Add an HTTP trigger for the SFTP To Go webhook
Once your Logic App has been created, go to the navigation bar on the left, expand the Development Tools group, and select Logic app designer. Repeat “Step 1. Set the trigger” from the first guide to add and set up the When an HTTP request is received trigger that will listen to incoming POST messages from SFTP To Go.
At the end of this step, you should have a functional trigger with its own HTTP URL:
It’s a good habit to give your triggers and actions descriptive names. This makes debugging complex workflows that much easier. You can use something like New SFTP file for the trigger.
Step 3. Test the Logic App endpoint
Let’s verify that the trigger works. Use curl to submit an empty POST message to the Logic App:
url -X POST 'INSERT_HTTP_URL_HERE'`Now open Run history in Development tools on the left. You should see an entry with a timestamp for the first time the trigger worked.
Step 4. Insert a JSON schema for the webhook payload
The Logic App won’t work with empty POST messages, though. SFTP To Go will send a payload for every new file on your account. So let’s add a JSON schema that some of the actions in our Logic App will use. Go back to the Logic App designer, open the trigger’s settings, and insert this code into the Request Body JSON Schema input field:
{
"type": "object",
"properties": {
"Data": {
"type": "object",
"properties": {
"Path": {
"type": "string"
},
"Size": {
"type": "integer"
}
}
}
}
}
Note that this is a very minimal version of the JSON schema, sufficient for this guide. We will only use the path. The file size is thrown in for good measure. However, adding more business logic will most likely require an enhanced version of the schema.
Step 5. Create the webhook in SFTP To Go
Now let’s temporarily switch to SFTP To Go. We’ll create an automation that will be feeding our Logic App with information about newly added files.
Log in to your SFTP To Go account and go to the Automation page. Click the + Add automation button and give the automation a distinguishable name:
Use the On a file or folder event trigger type and select File created as the event type:
Now let’s set up filter rules so that the webhook only fires when specific criteria are met.
Click + Add filter rule three times to add three rules, and then define them:
- Set the first rule to
Path - starts with - <username>/inbound/(e.g.,mike/inbound/) - Set the second rule to
Type - is - File. - Set the third rule to
Path - ends with - pdf.
This will make the automation distill all new file events to just the one where the file is created in your inbound folder and ends with pdf.
Finally, click + Add action, select Send webhook request in the list of available actions, paste the trigger’s URL into the Endpoint URL input box, then click Save automation.
If you like, you can do another test: upload a PDF file to the inbound folder on SFTP To Go and watch the workflow execute successfully.
Step 6. Connect over SFTP and get file content
Time to start getting new files from SFTP To Go. We are going to connect to the service over SFTP and use the file path that is part of the payload in the HTTP request.
Repeat Step 3. Set up SFTP connection and get file content from the previous guide up to the moment where you start entering an expression for the data path and use replace(coalesce(triggerBody()?['Data']?['Path'], ''), '<username>/', '/')instead. Replace <username> with the service account’s username you are going to use. Here is why we are doing this.
The path in the webhook’s payload starts with your username, e.g., mike/inbound/file.pdf. But when you log in as mike over SFTP, the SFTP server automatically puts you inside the /mike folder as your starting point. So you need to remove the path prefix that is the username to help the SFTP connector locate the file.
The expression above grabs the path from Data.Path, handles any null/test states with coalesce, and strips out the <username>/ prefix so the SFTP connector points directly to /inbound/….
One other thing worth mentioning is the credentials for the SFTP connection. If you have followed the first guide before and created this new app in the same resource group as the first one, you won’t need to establish a new SFTP connection. The Logic App will simply connect to the one you already have in API connections. That’s perfectly sensible for a quick test.
That said, you should be mindful of what credentials are going to be used in production. If you are going to use a different set of those, click Change Connection below, then click Create new and submit the new credentials.
Step 7. Connect to SharePoint and save files
Add a new action: Create file from the SharePoint connector. Sign in to your SharePoint account via OAuth, select the site address, and choose the folder where you will be saving files from SFTP To Go.
After that, let’s set up the filename and content.
For the filename, use the following expression:
last(split(coalesce(triggerBody()?['Data']?['Path'], ''), '/'))This expression splits the path string by slashes and extracts just the file name to save it under the exact same name in SharePoint.
We are nearly done. It’s time to select the actual data to save to a file on SharePoint. Open the expression editor for File Content, switch to Dynamic content, and select File Content from the Get file content using path (or whichever name you gave to that action).
After that, click Publish to run the Logic App you created.
Step 8. Test your SFTP To Go to SharePoint workflow
Let’s try to upload a PDF file to the folder that the automation on SFTP To Go is watching:
The file should show up with that exact name in SharePoint:
Further refinements for your Logic App workflow
You can enhance this simplistic workflow in numerous ways.
Route SFTP files to different SharePoint folders
Instead of saving everything to one SharePoint folder, you can route files to different folders depending on the file extension.
The first step is to decide on the logic. Right now, the webhook only gets triggered if the file type is PDF, then the app picks whatever file matches the spec and saves it to a SharePoint folder. If you start supporting two or more file types, do you only allow those, or do you route files of the “supported” types to dedicated folders and then route files of all “unsupported” types elsewhere?
This affects how you configure filters in the automation that triggers the webhook and how you refine your Logic App.
Let’s pick the configuration where you want to route files the following way:
- CSVs go to the
spreadsheetsfolder on SharePoint - PDFs go to the
PDFsfolder on SharePoint - All other file types go to the
unspecifiedfolder on SharePoint.
Start by removing the automation filter that looks for files that end with pdf. After that, add a new Switch action immediately after Get file content using path and set its On value to last(split(toLower(triggerBody()?['Data']?['Path']), '.')) using an expression.
Now, create a case for each type you accept: csv and pdf, each with its own SharePoint Create file action that uses the same set of expressions as the original Create file action. Leave the Default case to save to an Unsorted folder on SharePoint.
Get alerts when the workflow fails
Running something on automation shouldn’t mean running it unsupervised. If a workflow fails, it’s a good idea to get a notification. In Logic Apps, you do this by wrapping the main work in one Scope and adding a second Scope that only runs when the first one fails.
- Put the SFTP and SharePoint actions inside a
Scopeaction. - Add a second
Scopeafter the first one. - Open the second scope’s
Configure run aftermenu, tickHas failedandHas timed out, and untickIs successful. - Inside the second scope, add a
Post message in a chat or channelaction from the Teams connector (orSend an email (V2)from Outlook). Include the file name and a link to the failed run.
The second scope now stays idle unless something in the first one breaks.
Limit concurrent Logic App runs
By default, the HTTP trigger starts a separate run for every request it receives. If a third party uploads hundreds of files all at once, SFTP To Go will send hundreds of webhooks, and Logic Apps will have to process them all in parallel. That can hit throttling limits on both the SFTP-SSH and SharePoint connectors (150 and 600 API calls a minute, respectively), and some runs will fail. To prevent this, open the trigger’s Settings tab, enable Concurrency control, and set a limit on parallel runs. Logic Apps caps this at 100, so pick a lower number that your SharePoint site and connectors can comfortably handle.
Handle duplicate files in SharePoint
Just like SFTP To Go, SharePoint supports version history for files. When you upload a file to a folder where a file with that name already exists, SharePoint will create a new revision rather than simply overwrite the file.
This can be very helpful, but it also may not match your preferred way of handling revisions. If you would rather have separate files with a timestamp in the name, follow our first Logic Apps guide that explains how to extract modification timestamp metadata from SFTP To Go and append it to the original filename using the Compose action.
Wrapping up
You now have a Logic App that waits for SFTP To Go to report a new file, fetches it via SFTP, and saves it to SharePoint under its original name. A trigger and two actions handle the entire workflow.
The next decision is where each piece of logic should live. If your logic is straightforward, filtering by folder or file extension is done better in the SFTP To Go automation, especially on the Consumption plan where discarding submitted files means paying for workflow runs that could have been avoided entirely.
Meanwhile, anything that depends on the SharePoint side, such as destination folders or how to treat duplicates, belongs in the Logic App.
Deciding this early keeps the logic from being split across two places that are hard to debug together.
Before you rely on the workflow in production, consider adding some safeguards as described in the previous section, especially failure alerts. Use a dedicated service account rather than a personal one for both SFTP To Go and SharePoint.
Frequently asked questions
No. SFTP To Go sends a webhook only when a file is created or reuploaded, so files that were already there won't trigger the workflow. To move them, build a separate one-off Logic App that lists the files in the folder with the List files in folder action from the SFTP-SSH connector and loops over them with the same Get file content using path and Create file actions.
By default, Logic Apps perform the next step only if the previous action has been successful, so that part is already covered for you. What you should check for before using the Delete file action is whether the actual files match. To do that, use a Condition action that compares the size of the saved file with the Size value from the webhook payload (triggerBody()?['Data']?['Size']), and only delete only if they match.
There is a slight, if annoying, mismatch between the name of the SharePoint library in the UI and its actual path. The UI name is Documents, but its path is Shared Documents. If you type the folder path manually in the Create file action instead of using the folder picker, start with /Shared Documents.
The connector caches the state of the SharePoint library the first time it connects. It takes a while for this cache to be automatically invalidated. You don’t need to wait for it, though. If you know where inside Shared Documents the folder is, simply type the path in the input box.
On the Consumption plan that we used in this guide, Azure bills for each action execution, so every file adds a handful of billable actions. The webhook only starts a run when a file arrives and has a filter for the right file extension. This means you don't pay for empty polling checks or irrelevant results. Costs still add up with high volumes of files, so check the current Azure Logic Apps pricing, estimate it against your expected traffic, and design your Logic App around that.