private static void SyncToS3() { S3Connection S3Conn = new S3Connection(Keys.AwsAccessKeyId, Keys.AwsSecretAccessKey); S3Bucket Bucket = new S3Bucket(S3Conn, BucketName); string strLocalPath = "c:\\testing\\"; Utils.ListDirectory(strLocalPath); foreach (var S3Obj in Bucket.Keys) { string StrPathName = S3Obj.Key.Replace("/", "\\"); string StrFileName = strLocalPath + "\\" + S3Obj.Key; FileInfo FiLocal = new FileInfo(StrFileName); // Make sure the directory is already created. if (!Directory.Exists(FiLocal.DirectoryName)) { try { Directory.CreateDirectory(FiLocal.DirectoryName); } catch (Exception ex) { throw ex; } showNotifyMessage("Directory Created", "Directory \"" + FiLocal.DirectoryName + "\" created successfully."); } // Is a directory, skip it. if (S3Obj.Key.EndsWith("/")) { Utils.AlDirectories.Remove(FiLocal.DirectoryName); continue; } if (!FiLocal.Exists) { S3Obj.Get(FiLocal.FullName); showNotifyMessage("File Downloaded", FiLocal.FullName); } else { Utils.AlFiles.Remove(FiLocal.FullName); DateTime lastWriteTime = FiLocal.LastWriteTime; if (lastWriteTime > S3Obj.LastModified) { string LocalFileETag = Utils.Md5(StrFileName); if (!LocalFileETag.ToLower().Equals(S3Obj.ETag)) { S3Obj.Upload(FiLocal.FullName); showNotifyMessage("S3 File Updated", FiLocal.FullName + ", \nEtag:" + LocalFileETag.ToLower() + "\nS3ETag" + S3Obj.ETag); } continue; } if (lastWriteTime < S3Obj.LastModified) { string LocalFileETag = Utils.Md5(StrFileName); if (!LocalFileETag.ToLower().Equals(S3Obj.ETag)) { S3Obj.Get(FiLocal.FullName); showNotifyMessage("Local File Updated", FiLocal.FullName + ", \nEtag:" + LocalFileETag.ToLower() + "\nS3ETag" + S3Obj.ETag); } continue; } } } foreach (var StrPath in Utils.AlDirectories) { string S3Key = Utils.GetKeyByPath((string)StrPath, strLocalPath); S3Key += "/"; S3Object S3Obj = new S3Object(S3Conn, BucketName, S3Key); S3Obj.Upload(StrPath.ToString()); Console.WriteLine("[local] Upload Directory" + S3Key); } foreach (var StrPath in Utils.AlFiles) { string S3Key = Utils.GetKeyByPath((string)StrPath, strLocalPath); S3Object S3Obj = new S3Object(S3Conn, BucketName, S3Key); S3Obj.Upload(StrPath.ToString()); Console.WriteLine("[local] Upload Directory" + S3Key); } }
private static void S3CreateFileInDirectoryTest() { string S3Key = "mydir/WGAErrLog.txt"; S3Connection S3Conn = new S3Connection(Keys.AwsAccessKeyId, Keys.AwsSecretAccessKey); S3Bucket Bucket = new S3Bucket(S3Conn, BucketName); try { S3Object S3Obj = new S3Object(S3Conn, BucketName, S3Key); S3Obj.Upload(StrLocalPath + "\\WGAErrLog.txt"); Console.WriteLine(S3Key); } catch (Exception ee) { Console.WriteLine("Error:" + ee.Message); } }
public void DeleteDirectory(string DirectoryName) { foreach (var S3Obj in this.Keys) { if (S3Obj.Key.ToLower().StartsWith(DirectoryName.ToLower())) { S3Object S3O = new S3Object(Connection, m_name, S3Obj.Key); S3O.Delete(); } } }
private void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e) { string S3Key = Utils.GetKeyByPath(e.FullPath, strLocalPath); string FileType = "File"; if (Directory.Exists(e.FullPath)) { S3Key += "/"; FileType = "Directory"; } try { if (FileType != "Directory") { S3Object S3Obj = new S3Object(S3Conn, BucketName, Utils.GetKeyByPath(e.OldFullPath, strLocalPath)); S3Obj.Delete(); S3Obj = new S3Object(S3Conn, BucketName, S3Key); S3Obj.Upload(e.FullPath); } else { Bucket.DeleteDirectory(S3Key); Utils.ListDirectory(e.FullPath); foreach (var StrPath in Utils.AlDirectories) { string S3KeyOfDir = Utils.GetKeyByPath((string)StrPath, strLocalPath); S3KeyOfDir += "/"; S3Object S3Obj = new S3Object(S3Conn, BucketName, S3KeyOfDir); S3Obj.Upload(StrPath.ToString()); showNotifyMessage("Upload Directory", (string)StrPath); } foreach (var StrPath in Utils.AlFiles) { string S3KeyOfDir = Utils.GetKeyByPath((string)StrPath, strLocalPath); S3Object S3Obj = new S3Object(S3Conn, BucketName, S3KeyOfDir); S3Obj.Upload(StrPath.ToString()); showNotifyMessage("Upload File", (string)StrPath); } } this.showNotifyMessage(FileType + " Renamed", S3Key); } catch (Exception ee) { showErrorMessage("Error", ee.Message); } }
private void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e) { string S3Key = Utils.GetKeyByPath(e.FullPath, strLocalPath); try { S3Object S3Obj = new S3Object(S3Conn, BucketName, S3Key); S3Obj.Delete(); Bucket.DeleteDirectory(S3Key + "/"); this.showNotifyMessage("File or Directory Deleted", e.FullPath); } catch (Exception ee) { showErrorMessage("Error", ee.Message); } }
private void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e) { string S3Key = Utils.GetKeyByPath(e.FullPath, strLocalPath); string FileType = "File"; if (Directory.Exists(e.FullPath)) { S3Key += "/"; FileType = "Directory"; if (e.ChangeType.ToString() == "Changed") return; } try { S3Object S3Obj = new S3Object(S3Conn, BucketName, S3Key); S3Obj.Upload(e.FullPath); this.showNotifyMessage(FileType + " " + e.ChangeType, S3Key); } catch (Exception ee) { showErrorMessage("Error", ee.Message); } }