SSH.NET SFTP Get a list of directories and files recursively

后端 未结 5 1810
不思量自难忘°
不思量自难忘° 2021-02-13 13:50

I am using Renci.SshNet library to get a list of files and directories recursively by using SFTP. I can able to connect SFTP site but I am not sure how to get a list of director

5条回答
  •  既然无缘
    2021-02-13 14:25

    Here is a full class. It's .NET Core 2.1 Http trigger function app (v2)

    I wanted to get rid of any directories that start with '.', cause my sftp server has .cache folders and .ssh folders with keys. Also didn't want to have to deal with folder names like '.' or '..'

    What I will end up doing is projecting the SftpFile into a type that I work with and return that to the caller (in this case it will be a logic app). I'll then pass that object into a stored procedure and use OPENJSON to build up my monitoring table. This is basically the first step in creating my SFTP processing queue that will move files off my SFTP folder and into my Data Lake (blob for now until I come up with something better I guess).

    The reason I used .WorkingDirectory is because I created a user with home directory as '/home'. This lets me traverse all of my user folders. My app doesn't need to have a specific folder as a starting point, just the user 'root' so to speak.

    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.Extensions.Logging;
    using Renci.SshNet;
    using Renci.SshNet.Sftp;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace SFTPFileMonitor
    {
        public class GetListOfFiles
        {
            [FunctionName("GetListOfFiles")]
            public async Task RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req, ILogger log)
            {
                log.LogInformation("C# HTTP trigger function processed a request.");
    
                List zFiles;
                int fileCount;
                decimal totalSizeGB;
                long totalSizeBytes;
    
                using (SftpClient sftpClient = new SftpClient("hostname", "username", "password"))
                {
                    sftpClient.Connect();
                    zFiles = await GetFiles(sftpClient, sftpClient.WorkingDirectory, new List());
                    fileCount = zFiles.Count;
                    totalSizeBytes = zFiles.Sum(l => l.Length);
                    totalSizeGB = BytesToGB(totalSizeBytes);
                }
    
                return new OkObjectResult(new { fileCount, totalSizeBytes, totalSizeGB, zFiles });
            }
            private async Task> GetFiles(SftpClient sftpClient, string directory, List files)
            {
                foreach (SftpFile sftpFile in sftpClient.ListDirectory(directory))
                {
                    if (sftpFile.Name.StartsWith('.')) { continue; }
    
                    if (sftpFile.IsDirectory)
                    {
                        await GetFiles(sftpClient, sftpFile.FullName, files);
                    }
                    else
                    {
                        files.Add(sftpFile);
                    }
                }
                return files;
            }
            private decimal BytesToGB(long bytes)
            {
                return Convert.ToDecimal(bytes) / 1024 / 1024 / 1024;
            }
        }
    }
    

提交回复
热议问题