C# How to refer to Azure BLOB storage container in session.Getfiles

Advertisement

ehs
Joined:
Posts:
1

C# How to refer to Azure BLOB storage container in session.Getfiles

Being new to writing Azure Functions in C#, I have a problem using the WinSCP method session.GetFiles trying to put the retrieved files in a Azure BLOB Storage container.

My code works fine when the toPath is set to a directory on my PC and the code is running locally, but when I publish to an Azure Function, I don't know how to replace the toPath with something that streams the files to my Azure BLOB storage container.

I read some place that the GetFiles method supported streaming, but when I try using my container variable, VS Code tells me that the second parameter in the method must be a string.

Can anyone please provide a solution to this challenge?
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using WinSCP;
using Azure.Storage.Blobs;
 
namespace myCompany.Function
{
    public static class HttpTrigger1
    {
        [FunctionName("HttpTrigger1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log,  ExecutionContext context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
 
            // Test 07-01-2023 Begin
            var BLOBStorageConnectionString = “MyConnectionString”;
            var BLOBStorageContainerName = "returnfiles";
            var container = new BlobContainerClient(BLOBStorageConnectionString,BLOBStorageContainerName);
            var toPath = "C:/returnfiles/";
            // Test 07-01-2023 End
 
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "xxx.xxx.xxx.xxx",
                UserName = "xxxxxx",
                SshHostKeyFingerprint = "ssh-rsa 1024 03:XX:XX>..",
                SshPrivateKeyPath = Path.Combine(context.FunctionAppDirectory,"MyPrivateKey.ppk")
                
            };
                
            using (Session session = new Session())
            {
                session.ExecutablePath = Path.Combine(context.FunctionAppDirectory, "winscp.exe");   
                
                // Connect
                session.Open(sessionOptions);
 
                // Download files
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;
 
                TransferOperationResult transferResult;
                transferResult =
                    session.GetFiles("/Inbound/NORXXXXXXXXX/*_OUT.*", toPath, false, transferOptions);
 
                // Throw on any error
                transferResult.Check();
 
                // Print results
                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    Console.WriteLine("Download of {0} succeeded", transfer.FileName);
                }
            }
        }
    }
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,262
Location:
Prague, Czechia

Re: C# How to refer to Azure BLOB storage container in session.Getfiles

I do not have any experience with Azure Blob storage.
But it seems that something like this should do:
using (var sftpFileStream = session.GetFile("/Inbound/NORXXXXXXXXX/filename"))
{
    container.UploadBlob(blobName, sftpFileStream);
}
(once you make it working, please post your final code)

Reply with quote

Advertisement

You can post new topics in this forum