示例#1
0
        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);
            }
        }
示例#2
0
 private static void S3DirectoryDeleteTest()
 {
     S3Connection S3Conn = new S3Connection(Keys.AwsAccessKeyId, Keys.AwsSecretAccessKey);
     S3Bucket Bucket = new S3Bucket(S3Conn, BucketName);
     foreach (var S3Obj in Bucket.Keys)
         Console.WriteLine(S3Obj.Key + "," + S3Obj.LastModified + "," + S3Obj.Size / 1024 + "KB");
     try
     {
         Bucket.DeleteDirectory("mydir/");
     }
     catch (Exception ee)
     {
         Console.WriteLine("Error:" + ee.Message);
     }
 }
示例#3
0
 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);
     }
 }
示例#4
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            if ((txtLocalPath.Text.Length == 0) || (txtAWSID.Text.Length == 0)
                 || (txtAWSKey.Text.Length == 0) || (txtBucket.Text.Length == 0))
            {
                MessageBox.Show("Please input the AWS information, Bucket Name, and Local Path.", "Please define AWS information, Bucket name, Local Path ...", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            RegistryKey rk = Registry.CurrentUser;
            RegistryKey rkOpen = rk.CreateSubKey(REGISTRY_KEY);
            rkOpen.SetValue("LocalPath", txtLocalPath.Text);
            rkOpen.SetValue("AWSID", txtAWSID.Text);
            rkOpen.SetValue("AWSKey", txtAWSKey.Text);
            rkOpen.SetValue("Bucket", txtBucket.Text);
            BucketName = txtBucket.Text;
            strLocalPath = txtLocalPath.Text;
            //Create the directory if it is not created
            if (!Directory.Exists(strLocalPath))
            {
                try
                {
                    Directory.CreateDirectory(strLocalPath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            TopMost = false;
            this.WindowState = FormWindowState.Minimized;
            this.Hide();
            notifyIcon.Text = "Amazon S3 Sync [running]";

            // Sync the directory first.
            S3Conn = new S3Connection(txtAWSID.Text, txtAWSKey.Text);
            Bucket = new S3Bucket(S3Conn, BucketName);

            try
            {
                CheckBucket();
                //SyncS3();
                fileSystemWatcher.Path = strLocalPath;
            }
            catch (ArgumentException ae)
            {
                showErrorMessage("Error", ae.Message);
            }
            catch (Exception ee)
            {
                showErrorMessage("Error", ee.Message);
            }
            btnStart.Enabled = false;
            txtAWSID.Enabled = false;
            txtAWSKey.Enabled = false;
            txtBucket.Enabled = false;
            localDirBrowser.Enabled = false;
        }