Created
June 20, 2020 15:13
-
-
Save changhuixu/e7ea4d44e0c7684e0ebf9889abeea27d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SftpService | |
| { | |
| private readonly ILogger<SftpService> _logger; | |
| private readonly SftpConfig _config; | |
| public SftpService(ILogger<SftpService> logger, SftpConfig sftpConfig) | |
| { | |
| _logger = logger; | |
| _config = sftpConfig; | |
| } | |
| public IEnumerable<SftpFile> ListAllFiles(string remoteDirectory = ".") | |
| { | |
| using var client = new SftpClient(_config.Host, _config.Port == 0 ? 22 : _config.Port, _config.UserName, _config.Password); | |
| try | |
| { | |
| client.Connect(); | |
| return client.ListDirectory(remoteDirectory); | |
| } | |
| catch (Exception exception) | |
| { | |
| _logger.LogError(exception, $"Failed in listing files under [{remoteDirectory}]"); | |
| return null; | |
| } | |
| finally | |
| { | |
| client.Disconnect(); | |
| } | |
| } | |
| public void UploadFile(string localFilePath, string remoteFilePath) | |
| { | |
| using var client = new SftpClient(_config.Host, _config.Port == 0 ? 22 : _config.Port, _config.UserName, _config.Password); | |
| try | |
| { | |
| client.Connect(); | |
| using var s = File.OpenRead(localFilePath); | |
| client.UploadFile(s, remoteFilePath); | |
| _logger.LogInformation($"Finished uploading file [{localFilePath}] to [{remoteFilePath}]"); | |
| } | |
| catch (Exception exception) | |
| { | |
| _logger.LogError(exception, $"Failed in uploading file [{localFilePath}] to [{remoteFilePath}]"); | |
| } | |
| finally | |
| { | |
| client.Disconnect(); | |
| } | |
| } | |
| public void DownloadFile(string remoteFilePath, string localFilePath) | |
| { | |
| using var client = new SftpClient(_config.Host, _config.Port == 0 ? 22 : _config.Port, _config.UserName, _config.Password); | |
| try | |
| { | |
| client.Connect(); | |
| using var s = File.Create(localFilePath); | |
| client.DownloadFile(remoteFilePath, s); | |
| _logger.LogInformation($"Finished downloading file [{localFilePath}] from [{remoteFilePath}]"); | |
| } | |
| catch (Exception exception) | |
| { | |
| _logger.LogError(exception, $"Failed in downloading file [{localFilePath}] from [{remoteFilePath}]"); | |
| } | |
| finally | |
| { | |
| client.Disconnect(); | |
| } | |
| } | |
| public void DeleteFile(string remoteFilePath) | |
| { | |
| using var client = new SftpClient(_config.Host, _config.Port == 0 ? 22 : _config.Port, _config.UserName, _config.Password); | |
| try | |
| { | |
| client.Connect(); | |
| client.DeleteFile(remoteFilePath); | |
| _logger.LogInformation($"File [{remoteFilePath}] deleted."); | |
| } | |
| catch (Exception exception) | |
| { | |
| _logger.LogError(exception, $"Failed in deleting file [{remoteFilePath}]"); | |
| } | |
| finally | |
| { | |
| client.Disconnect(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment