Differences

This shows you the differences between the selected revisions of the page.

guide_microsoft_azure_function_sftp 2019-08-20 guide_microsoft_azure_function_sftp 2022-11-14 (current)
Line 1: Line 1:
====== SFTP/FTPS file transfers in Microsoft Azure Function ====== ====== SFTP/FTPS file transfers in Microsoft Azure Function ======
-To use [[library|WinSCP .NET assembly]] to implement an SFTP/FTP functionality in [[https://docs.microsoft.com/en-us/azure/azure-functions/|Microsoft Azure Function]], just add [[library_install#nuget|WinSCP NuGet package]] to your Azure Functions project in Visual Studio and then use any [[library_examples|standard WinSCP code]].+To use [[library|WinSCP .NET assembly]] to implement an SFTP/FTP functionality in [[https://learn.microsoft.com/en-us/azure/azure-functions/|Microsoft Azure Function]], just add [[library_install#nuget|WinSCP NuGet package]] to your Azure Functions project in Visual Studio and then use any [[library_examples|standard WinSCP code]].
The only problem you will face is, that for some reason, when publishing the project to an Azure storage, ''winscp.exe'' dependency is stored to the root folder of the function, instead of the ''bin'' subfolder along with other binaries (particularly the ''WinSCPnet.dll''). The only problem you will face is, that for some reason, when publishing the project to an Azure storage, ''winscp.exe'' dependency is stored to the root folder of the function, instead of the ''bin'' subfolder along with other binaries (particularly the ''WinSCPnet.dll'').
-To overcome that, you need to use [[library_session#executablepath|''Session.ExecutablePath'']] to point to the actual location of ''winscp.exe''. You can use ''ExecutionContext.FunctionAppDirectory'' to refer to the root folder of the function. To get an instance of the [[https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#executioncontext|''ExecutionContext'']], add new parameter to the signature of [[https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#methods-recognized-as-functions|your entry method]] (usually ''Run''):+To overcome that, you need to use [[library_session#executablepath|''Session.ExecutablePath'']] to point to the actual location of ''winscp.exe''. You can use ''ExecutionContext.FunctionAppDirectory'' to refer to the root folder of the function. To get an instance of the [[https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#executioncontext|''ExecutionContext'']], add new parameter to the signature of [[https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#methods-recognized-as-functions|your entry method]] (usually ''Run'')
 + 
 +The following example is for version 2.x of Azure Functions:
<code csharp> <code csharp>
[FunctionName("Function1")] [FunctionName("Function1")]
public static void Run( public static void Run(
-    [TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log, ExecutionContext context)+    [TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log, ExecutionContext context)
{ {
-    SessionOptions sessionOptions = new SessionOptions(); +    try
-    // setup sessionOptions  +
- +
-    using (Session session = new Session())+
    {     {
-        // Look for winscp.exe in the root folder of the function +        log.LogInformation("Starting");
-        session.ExecutablePath = Path.Combine(context.FunctionAppDirectory, "winscp.exe");+
-        session.Open(sessionOptions); +        SessionOptions sessionOptions = new SessionOptions(); 
-········ +        // setup sessionOptions  
- ·······// ...  + 
 +        using (Session session = new Session()) 
 +        { 
 +            // Look for winscp.exe in the root folder of the function 
 +            session.ExecutablePath = Path.Combine(context.FunctionAppDirectory, "winscp.exe"); 
 + 
 +            log.LogInformation("Connecting"); 
 +············session.Open(sessionOptions); 
 + 
 + ···········// ... 
 +       } 
 +       log.LogInformation("Done"); 
 +    } 
 +    catch (Exception e) 
 +    { 
 +        log.LogError(e, "Error");
    }     }
} }
</code> </code>
 +For version 1.x of Azure functions, the code is basically the same. Just for logging, you need to use ''TraceWriter'' instead of ''ILogger''. Read about  [[https://learn.microsoft.com/en-us/azure/azure-functions/migrate-version-1-version-4|Migrating from version 1.x]].

Last modified: by martin