求助:如何通过Function App将SFTP中的文件迁移至Azure Blob?
Hey there! Since you’ve already got this workflow working in Logic Apps, shifting it to Azure Function App is totally straightforward. Let’s walk through the steps to get your sample.xml migration running smoothly with the external file trigger you’ve configured.
1. Set Up Required Dependencies
First, make sure your Function App has the necessary packages to interact with SFTP and Azure Blob Storage:
- For C# Functions: Install
Renci.SshNet(for SFTP operations) andAzure.Storage.Blobs(for Blob interactions) via NuGet. - For Python Functions: Install
paramiko(SFTP) andazure-storage-blob(Blob) using pip.
2. Core Function Code (C# Example)
Here’s a complete example using the external file trigger. This code will trigger when sample.xml is detected in your specified SFTP path, then copy the file directly to Azure Blob Storage:
using System.IO; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; public static class SftpToBlobMigration { [FunctionName("SftpToBlobTransfer")] public static void Run( // External File Trigger pointing to your SFTP sample.xml path [ExternalFileTrigger("sftp://your-sftp-server:22/your-sftp-folder/sample.xml", Connection = "SftpConnectionString")] Stream sftpFileStream, // Blob Output binding to your target container [Blob("your-blob-container/sample.xml", FileAccess.Write, Connection = "AzureWebJobsStorage")] Stream blobOutputStream, ILogger log) { log.LogInformation($"Starting transfer of sample.xml from SFTP to Blob Storage"); // Copy the SFTP file stream directly to the Blob output stream sftpFileStream.CopyTo(blobOutputStream); log.LogInformation($"Successfully transferred sample.xml to Azure Blob Storage!"); } }
3. Key Configuration Steps
SFTP Connection String: Add a new app setting named
SftpConnectionStringin your Function App’s Configuration. Format:Host=your-sftp-host;Port=22;Username=your-sftp-username;Password=your-sftp-passwordFor key-based authentication, replace the password with
PrivateKey=path-to-your-private-key(ensure the key is in OpenSSH format).Blob Storage Connection: The
AzureWebJobsStorageapp setting should already point to your Blob Storage account’s connection string (make sure it has write permissions to your target container).Trigger Path Validation: Double-check the SFTP path in the trigger annotation matches exactly where
sample.xmlis stored—typos here will prevent the trigger from firing.
4. Testing & Troubleshooting
- Test the Trigger: Upload
sample.xmlto your SFTP path, then check your Function App’s Logs (under Monitor > Logs in the Azure Portal) to confirm the function ran successfully. - Network Issues: Ensure your SFTP server allows inbound connections from your Function App’s outbound IP addresses (find these in your Function App’s Networking settings).
- Blob Permissions: Verify your target Blob container exists and the connection string has
Storage Blob Data Contributoror equivalent permissions.
内容的提问来源于stack exchange,提问作者sreeprasad k s

