Last active
January 5, 2021 07:40
-
-
Save MarshalOfficial/698b7cc45f8a26bc72ad1a7d0fcf5536 to your computer and use it in GitHub Desktop.
SCP Sample C#, it will copy all the files in target folder to a remote server target location via scp and parallelism with max thread count is manageable
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
| using System; | |
| using System.Collections.Generic; | |
| using System.Configuration; | |
| using System.IO; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using WinSCP; | |
| namespace YOURPROJECTNAMESPACE | |
| { | |
| public class SCPEngine | |
| { | |
| private string filesPath; | |
| public SCPEngine() | |
| { | |
| filesPath = Path.Combine(Extension.Extensions.AssemblyDirectory, "Storage"); | |
| } | |
| public async Task ExecuteAsync() | |
| { | |
| try | |
| { | |
| if (!Extension.Extensions.CheckConnection(Program.GetSettingValue("DestServerIP"), int.Parse(Program.GetSettingValue("DestServerPort")))) | |
| { | |
| Program.Logger.Info($"dest server is unavailable on ip & port that you put in the app config, please check the issue with your network administrator."); | |
| return; | |
| } | |
| if (!Directory.Exists(filesPath)) | |
| { | |
| Program.Logger.Info($"there is no storage folder to processing for SCP."); | |
| return; | |
| } | |
| var tasks = new List<Task>(); | |
| DirectoryInfo dir = new DirectoryInfo(filesPath); | |
| foreach (var file in dir.GetFiles("*.txt").OrderBy(l => l.FullName)) | |
| { | |
| tasks.Add(new Task(() => Upload(file))); | |
| } | |
| var maxparallelcopy = int.Parse(Program.GetSettingValue("MaxCopyThread")); | |
| while (tasks.Any(l => !l.IsCompleted)) | |
| { | |
| var innserTasks = tasks.Where(l => !l.IsCompleted).Take(maxparallelcopy).ToList(); | |
| innserTasks.ForEach(l => l.Start()); | |
| Task.WaitAll(innserTasks.ToArray()); | |
| } | |
| Task.WaitAll(tasks.ToArray()); | |
| } | |
| catch (Exception ex) | |
| { | |
| Program.Logger.Error(ex); | |
| } | |
| } | |
| private bool Upload(FileInfo file) | |
| { | |
| try | |
| { | |
| var lines = File.ReadLines(file.FullName); | |
| if (lines == null || lines.Count() == 0) | |
| { | |
| file.Delete(); | |
| return true; | |
| } | |
| Session sess = new Session(); | |
| var finger = sess.ScanFingerprint(new SessionOptions | |
| { | |
| Protocol = Protocol.Sftp, | |
| HostName = Program.GetSettingValue("DestServerIP"), | |
| UserName = Program.GetSettingValue("DestServerOSUser"), | |
| Password = Program.GetSettingValue("DestServerOSPassword") | |
| }, "SHA-256"); | |
| SessionOptions sessionOptions = new SessionOptions | |
| { | |
| Protocol = Protocol.Sftp, | |
| HostName = Program.GetSettingValue("DestServerIP"), | |
| UserName = Program.GetSettingValue("DestServerOSUser"), | |
| Password = Program.GetSettingValue("DestServerOSPassword"), | |
| SshHostKeyFingerprint = finger | |
| }; | |
| using (Session session = new Session()) | |
| { | |
| session.Open(sessionOptions); | |
| TransferOptions transferOptions = new TransferOptions(); | |
| transferOptions.TransferMode = TransferMode.Binary; | |
| //transferOptions.ResumeSupport.State = TransferResumeSupportState.Off; | |
| TransferOperationResult transferResult; | |
| Program.Logger.Info($"aattempt to upload: " + file.Name + " ..."); | |
| transferResult = | |
| session.PutFiles(file.FullName, $"/{session.HomePath}/parentfoldername/Storage/", false, transferOptions); | |
| transferResult.Check(); | |
| foreach (TransferEventArgs transfer in transferResult.Transfers) | |
| { | |
| Program.Logger.Info($"Upload of {transfer.FileName} succeeded"); | |
| } | |
| } | |
| Program.Logger.Info($"uploading: " + file.Name + "Finished."); | |
| file.Delete(); | |
| return true; | |
| } | |
| catch (Exception e) | |
| { | |
| Program.Logger.Error(e); | |
| return false; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment