コード例 #1
0
ファイル: Amazon.cs プロジェクト: mattwynyard/Geotagger_V2
 public void Dispose()
 {
     s3Client.Dispose();
     s3Client   = null;
     fileQueue  = null;
     errorQueue = null;
     uploadSum  = 0;
 }
コード例 #2
0
 public void Dispose()
 {
     if (_client != null)
     {
         _client.Dispose();
         _client = null;
     }
 }
コード例 #3
0
 public void Dispose()
 {
     if (_client != null)
     {
         _client.Dispose();
         Logger.Debug("Disposed of an AmazonS3Client");
     }
 }
コード例 #4
0
        protected override void DisposeObject(bool disposing)
        {
            if (disposing)
            {
                s3Client?.Dispose();

                transferUtility?.Dispose();
            }
        }
コード例 #5
0
        protected IAmazonS3 GetAmazonS3(string bucketName)
        {
            //Note, it would probably be fine to just generate this each time,
            //but this was the more conservative approach when refactoring
            //to allow a single client to access arbitrary buckets, thus requiring
            //appropriate change of access keys, thus requiring changing AmazonS3Client objects.
            if (bucketName != _previousBucketName)
            {
                if (_amazonS3 != null)
                {
                    _amazonS3.Dispose();
                }
                _amazonS3 = CreateAmazonS3Client(bucketName, _s3Config);

                _previousBucketName = bucketName;
            }
            return(_amazonS3);            // we keep this so that we can dispose of it later.
        }
コード例 #6
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposing || _storageClient == null)
            {
                return;
            }

            _storageClient.Dispose();
            _storageClient = null;
        }
コード例 #7
0
ファイル: Handler.cs プロジェクト: cythral/cfn
        public void Dispose()
        {
            if (disposed)
            {
                return;
            }

            s3Client.Dispose();
            s3Client = null;
            disposed = true;
        }
コード例 #8
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            s3Client?.Dispose();
            s3Client = null;
            disposed = true;
        }
コード例 #9
0
 public void Dispose()
 {
     try
     {
         _client?.Dispose();
     }
     catch
     {
         // ignored
     }
 }
コード例 #10
0
 /// <summary>
 /// Implements the Dispose pattern
 /// </summary>
 /// <param name="disposing">Whether this object is being disposed via a call to Dispose
 /// or garbage collected.</param>
 protected virtual void Dispose(bool disposing)
 {
     if (!this._isDisposed)
     {
         if (disposing && _s3Client != null && _shouldDispose)
         {
             _s3Client.Dispose();
             _s3Client = null;
         }
         this._isDisposed = true;
     }
 }
コード例 #11
0
 public void Dispose()
 {
     if (_transferUtility != null)
     {
         _transferUtility.Dispose();
         _transferUtility = null;
     }
     if (_amazonS3 != null)
     {
         _amazonS3.Dispose();
         _amazonS3 = null;
     }
 }
コード例 #12
0
 public static void Cleanup()
 {
     BaseClean();
     if (iamClient != null)
     {
         iamClient.Dispose();
         iamClient = null;
     }
     if (s3Client != null)
     {
         s3Client.Dispose();
         s3Client = null;
     }
 }
コード例 #13
0
 /// <summary>
 /// Class disposer
 /// </summary>
 public void Dispose()
 {
     try
     {
         if (S3client != null)
         {
             S3client.Dispose();
         }
     }
     catch
     {
     }
     S3client = null;
 }
コード例 #14
0
        private bool _disposedValue; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (_disposedValue)
            {
                return;
            }

            if (disposing)
            {
                AmazonS3.Dispose();
            }

            _disposedValue = true;
        }
コード例 #15
0
        public void Dispose()
        {
            if (s3Client == null)
            {
                return;
            }

            try
            {
                s3Client.Dispose();
                s3Client = null;
            }
            catch { }
        }
コード例 #16
0
        protected override void Dispose(bool dispose)
        {
            lock (this)
            {
                if (_Client != null)
                {
                    try
                    {
                        _Client.Dispose();
                    }
                    catch { }
                }

                _Client = null;
            }
        }
コード例 #17
0
 public void Dispose()
 {
     _s3Client?.Dispose();
 }
コード例 #18
0
 public void Dispose()
 {
     s3Client.Dispose();
 }
コード例 #19
0
 public void Dispose()
 {
     _client.Dispose();
 }
コード例 #20
0
 /// <summary>
 /// Disposes the file provider.
 /// </summary>
 public void Dispose()
 {
     amazonS3.Dispose();
 }
コード例 #21
0
 public void Dispose()
 {
     s3Client.Dispose();
     dynamoClient.Dispose();
 }
コード例 #22
0
 /// <summary>
 /// Disposes the file provider.
 /// </summary>
 public void Dispose() => amazonS3.Dispose();
コード例 #23
0
 public static void ClassCleanup()
 {
     s3Client.Dispose();
     BaseClean();
 }
コード例 #24
0
 public void Dispose()
 {
     try { s3client?.Dispose(); } catch { }
 }
コード例 #25
0
 public void Dispose()
 {
     amazonS3Client.Dispose();
 }
コード例 #26
0
 public void Dispose()
 {
     try { S3Client?.Dispose(); } catch { }
 }
コード例 #27
0
 /// <summary>
 ///     Called on graceful shutdown.
 /// </summary>
 public void Dispose() => client.Dispose();
コード例 #28
0
        private void ValidateS3Connection(S3StorageProviderSettingsPart part, IUpdateModel updater)
        {
            var provider = _storageProvider as S3StorageProvider;

            if (provider == null)
            {
                return;
            }

            IAmazonS3 client = null;

            try
            {
                if (part.UseCustomCredentials)
                {
                    bool valid = true;
                    if (string.IsNullOrWhiteSpace(part.AWSAccessKey))
                    {
                        updater.AddModelError("AWSAccessKey", T("Specify a value for AWS Access Key"));
                        valid = false;
                    }
                    if (string.IsNullOrWhiteSpace(part.AWSSecretKey))
                    {
                        updater.AddModelError("AWSAccessKey", T("Specify a value for AWS Secret Key"));
                        valid = false;
                    }
                    if (string.IsNullOrWhiteSpace(part.RegionEndpoint))
                    {
                        updater.AddModelError("AWSAccessKey", T("Specify a value for S3 Region Endpoint"));
                        valid = false;
                    }

                    if (!valid)
                    {
                        return;
                    }

                    client = provider.CreateClientFromCustomCredentials(part.AWSAccessKey, part.AWSSecretKey, Amazon.RegionEndpoint.GetBySystemName(part.RegionEndpoint));
                    if (client != null)
                    {
                        _notifier.Information(T("Connecting using custom credentials: OK"));
                    }
                }
                else
                {
                    var iamCredentials = provider.GetIAMCredentials();
                    client = provider.CreateClientFromIAMCredentials(iamCredentials);
                    if (client != null)
                    {
                        _notifier.Information(T("Connecting using IAM role: OK"));
                    }
                }

                // Check AWS credentials, bucket name and bucket permissions
                string bucketName = part.Record.BucketName;
                GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
                request.BucketName = bucketName;
                if (AWSConfigs.S3Config.UseSignatureVersion4)
                {
                    request.Expires = DateTime.Now.AddDays(6);
                }
                else
                {
                    request.Expires = new DateTime(2019, 12, 31);
                }
                request.Verb     = HttpVerb.HEAD;
                request.Protocol = Protocol.HTTP;
                string url = client.GetPreSignedURL(request);
                Uri    uri = new Uri(url);


                if (!AmazonS3Util.DoesS3BucketExist(client, bucketName))
                {
                    updater.AddModelError("Settings", T("Invalid bucket name. No bucket by the name {0} exists.", part.Record.BucketName));
                }
                else
                {
                    // Check for read/write permissions
                    var acl = client.GetACL(new GetACLRequest()
                    {
                        BucketName = bucketName
                    });

                    var grants = acl.AccessControlList.Grants;

                    if (!grants.Any(x => x.Permission == S3Permission.FULL_CONTROL))
                    {
                        if (!grants.Any(x => x.Permission == S3Permission.WRITE))
                        {
                            updater.AddModelError("Settings", T("You don't have write access to this bucket"));
                        }
                        if (!grants.Any(x => x.Permission == S3Permission.READ))
                        {
                            updater.AddModelError("Settings", T("You don't have read access to this bucket"));
                        }
                    }
                }

                _notifier.Information(T("All settings look okay"));
            }
            catch (AmazonS3Exception ex) {
                if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || ex.ErrorCode.Equals("InvalidSecurity")))
                {
                    updater.AddModelError("Settings", T("Invalid AWS credentials"));
                }
                else if (ex.ErrorCode != null && ex.ErrorCode.Equals("AccessDenied"))
                {
                    updater.AddModelError("Settings", T("Access denied. You don't have permission to access the bucket '{0}'", part.Record.BucketName));
                }
                else
                {
                    updater.AddModelError("Settings", T("Unknown error: {0}", ex.Message));
                }
            }
            finally {
                if (client != null)
                {
                    client.Dispose();
                }
            }
        }
コード例 #29
0
 public static void ClassCleanup()
 {
     AmazonS3Util.DeleteS3BucketWithObjects(s3Client, bucketName);
     s3Client.Dispose();
     BaseClean();
 }
コード例 #30
0
        public void RunBackupJob()
        {
            bool   blnBackupCompletedSuccessful = false;
            string strEmail         = "";
            string strEmailPassword = "";

            try
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(pvtstrLogFileDirectory + "DailyDBBackup_Info.txt", true))
                {
                    file.WriteLine("RunBackupJob ... " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }

                clsDBConnectionObjects = null;
                clsDBConnectionObjects = new InteractPayroll.clsDBConnectionObjects();

                DataSet DataSet = new System.Data.DataSet();

                StringBuilder strQry = new StringBuilder();

                strQry.Clear();

                strQry.AppendLine(" SELECT");
                strQry.AppendLine(" BACKUP_DATABASE_PATH");
                strQry.AppendLine(",BACKUP_GMAIL_ACCOUNT");
                strQry.AppendLine(",BACKUP_GMAIL_PASSWORD");
                strQry.AppendLine(",BACKUP_S3_BUCKET_NAME");
                strQry.AppendLine(",BACKUP_DB_IND");

                strQry.AppendLine(" FROM InteractPayroll.dbo.BACKUP_DATABASE_PATH");

                clsDBConnectionObjects.Create_DataTable(strQry.ToString(), DataSet, "Directory", -1);

                string strFileDirectory = DataSet.Tables["Directory"].Rows[0]["BACKUP_DATABASE_PATH"].ToString();
                strEmail         = DataSet.Tables["Directory"].Rows[0]["BACKUP_GMAIL_ACCOUNT"].ToString();
                strEmailPassword = DataSet.Tables["Directory"].Rows[0]["BACKUP_GMAIL_PASSWORD"].ToString();
                pvtstrBucketName = DataSet.Tables["Directory"].Rows[0]["BACKUP_S3_BUCKET_NAME"].ToString();

                string strDatabaseName   = "InteractPayroll";
                string strBackupFileName = strDatabaseName + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + "_Daily.bak";

                //Move Files Offshore
                client = new AmazonS3Client();

                string[] fileInteractPayrollPaths;

                FileInfo fileinfo;
                bool     blnFound = false;

                if (DataSet.Tables["Directory"].Rows[0]["BACKUP_DB_IND"].ToString() == "Y" ||
                    DateTime.Now.DayOfWeek == DayOfWeek.Saturday)
                {
                    //Backup InteractPayroll Master
                    fileInteractPayrollPaths = Directory.GetFiles(@strFileDirectory, @"*InteractPayroll_" + DateTime.Now.ToString("yyyyMMdd") + "_*.bak");

                    if (fileInteractPayrollPaths.Length == 0)
                    {
                        strQry.Clear();

                        strQry.AppendLine("BACKUP DATABASE " + strDatabaseName + " TO DISK = '" + strFileDirectory + "\\" + strBackupFileName + "'");

                        clsDBConnectionObjects.Execute_SQLCommand(strQry.ToString(), -1);
                    }

                    strQry.Clear();

                    strQry.AppendLine(" UPDATE InteractPayroll.dbo.BACKUP_DATABASE_PATH");
                    strQry.AppendLine(" SET BACKUP_DB_IND = 'N'");

                    clsDBConnectionObjects.Execute_SQLCommand(strQry.ToString(), -1);
                }

                fileInteractPayrollPaths = Directory.GetFiles(@strFileDirectory, @"*InteractPayroll_" + DateTime.Now.ToString("yyyyMMdd") + "_*.bak");

                if (fileInteractPayrollPaths.Length > 0)
                {
                    fileinfo = new FileInfo(fileInteractPayrollPaths[0]);

                    blnFound = FindFile(fileinfo.Name);

                    if (blnFound == false)
                    {
#if (DEBUG)
                        //Don't Want To Write Test DBs to Backup Bucket
#else
                        TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest
                        {
                            BucketName = pvtstrBucketName,
                            FilePath   = fileInteractPayrollPaths[0]
                        };

                        fileTransferUtilityRequest.Metadata.Add("fileName", fileinfo.Name);
                        fileTransferUtilityRequest.Metadata.Add("fileDesc", fileinfo.Name);

                        TransferUtility fileTransferUtility = new TransferUtility(client);

                        fileTransferUtility.Upload(fileTransferUtilityRequest);
#endif
                    }
                }

                strQry.Clear();

                strQry.AppendLine(" SELECT ");
                strQry.AppendLine(" COMPANY_NO ");

                strQry.AppendLine(" FROM InteractPayroll.dbo.COMPANY_LINK");

                strQry.AppendLine(" WHERE BACKUP_DB_IND = 1");

                clsDBConnectionObjects.Create_DataTable(strQry.ToString(), DataSet, "BackUpInfo", -1);

                //First Complete Todays Backups
                for (int intRow = 0; intRow < DataSet.Tables["BackUpInfo"].Rows.Count; intRow++)
                {
                    strDatabaseName = "InteractPayroll_" + Convert.ToInt32(DataSet.Tables["BackUpInfo"].Rows[intRow]["COMPANY_NO"]).ToString("00000");

                    strBackupFileName = strDatabaseName + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + "_Daily.bak";

                    strQry.Clear();

                    strQry.AppendLine("BACKUP DATABASE " + strDatabaseName + " TO DISK = '" + strFileDirectory + "\\" + strBackupFileName + "'");

                    clsDBConnectionObjects.Execute_SQLCommand(strQry.ToString(), -1);

                    strQry.Clear();

                    strQry.AppendLine(" UPDATE InteractPayroll.dbo.COMPANY_LINK");
                    strQry.AppendLine(" SET BACKUP_DB_IND = 0");
                    strQry.AppendLine(" WHERE COMPANY_NO = " + DataSet.Tables["BackUpInfo"].Rows[intRow]["COMPANY_NO"].ToString());

                    clsDBConnectionObjects.Execute_SQLCommand(strQry.ToString(), -1);

                    string[] fileOldDBPaths = Directory.GetFiles(strFileDirectory, strDatabaseName + "_*_Daily.bak");

                    if (fileOldDBPaths.Length > 5)
                    {
                        //Delete Old
                        Array.Sort(fileOldDBPaths);

                        for (int intFileRow = 0; intFileRow < fileOldDBPaths.Length - 5; intFileRow++)
                        {
                            File.Delete(fileOldDBPaths[intFileRow]);
                        }
                    }
                }

                //Complete Todays Offsite DB Backups
                string[] fileTodaysPaths = Directory.GetFiles(@strFileDirectory, @"*_" + DateTime.Now.ToString("yyyyMMdd") + "*.bak");

                if (fileTodaysPaths.Length > 0)
                {
                    for (int intFileCount = 0; intFileCount < fileTodaysPaths.Length; intFileCount++)
                    {
                        fileinfo = new FileInfo(fileTodaysPaths[intFileCount]);

                        blnFound = FindFile(fileinfo.Name);

                        if (blnFound == false)
                        {
#if (DEBUG)
                            //Don't Want To Write Test DBs to Backup Bucket
#else
                            TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest
                            {
                                BucketName = pvtstrBucketName,
                                FilePath   = fileTodaysPaths[intFileCount]
                            };

                            //LifecycleConfiguration a = new LifecycleConfiguration();


                            fileTransferUtilityRequest.Metadata.Add("fileName", fileinfo.Name);
                            fileTransferUtilityRequest.Metadata.Add("fileDesc", fileinfo.Name);
                            /////fileTransferUtilityRequest.Metadata.Add("expires","Tue, 16 Feb 2016 16:00:00 GMT");

                            TransferUtility fileTransferUtility = new TransferUtility(client);

                            fileTransferUtility.Upload(fileTransferUtilityRequest);
#endif
                        }
                    }
                }

                //Check Yesterdays DB Backups
                string[] fileYesterdaysPaths = Directory.GetFiles(@strFileDirectory, @"*_" + DateTime.Now.AddDays(-1).ToString("yyyyMMdd") + "*.bak");

                if (fileYesterdaysPaths.Length > 0)
                {
                    for (int intFileCount = 0; intFileCount < fileYesterdaysPaths.Length; intFileCount++)
                    {
                        fileinfo = new FileInfo(fileYesterdaysPaths[intFileCount]);

                        blnFound = FindFile(fileinfo.Name);

                        if (blnFound == false)
                        {
#if (DEBUG)
                            //Don't Want To Write Test DBs to Backup Bucket
#else
                            TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest
                            {
                                BucketName = pvtstrBucketName,
                                FilePath   = fileYesterdaysPaths[intFileCount]
                            };

                            fileTransferUtilityRequest.Metadata.Add("fileName", fileinfo.Name);
                            fileTransferUtilityRequest.Metadata.Add("fileDesc", fileinfo.Name);

                            TransferUtility fileTransferUtility = new TransferUtility(client);

                            fileTransferUtility.Upload(fileTransferUtilityRequest);
#endif
                        }
                    }
                }

                bool     blnFirstPass = true;
                string[] fileOldDaysPaths;

RunBackupJob_Continue:

                if (blnFirstPass == true)
                {
                    //Clean Up Old Backups Older than 1 Month Old
                    fileOldDaysPaths = Directory.GetFiles(@strFileDirectory, @"*_" + DateTime.Now.AddMonths(-1).ToString("yyyyMM") + "*.bak");
                }
                else
                {
                    fileOldDaysPaths = Directory.GetFiles(@strFileDirectory, @"*_" + DateTime.Now.AddMonths(-2).ToString("yyyyMM") + "*.bak");
                }

                foreach (string file in fileOldDaysPaths)
                {
                    int intOffset = file.IndexOf("InteractPayroll_");

                    if (intOffset > -1)
                    {
                        if (file.Length > intOffset + 30)
                        {
                            try
                            {
                                //Client Databases
                                DateTime myFileDateTime = DateTime.ParseExact(file.Substring(intOffset + 22, 8), "yyyyMMdd", null).AddMonths(1);

                                if (myFileDateTime < DateTime.Now)
                                {
                                    File.Delete(file);
                                }
                            }
                            catch
                            {
                                //InteractPayroll Master

                                try
                                {
                                    DateTime myFileDateTime = DateTime.ParseExact(file.Substring(intOffset + 16, 8), "yyyyMMdd", null).AddMonths(1);

                                    if (myFileDateTime < DateTime.Now)
                                    {
                                        File.Delete(file);
                                    }
                                }
                                catch
                                {
                                }
                            }
                        }
                    }
                }

                if (blnFirstPass == true)
                {
                    blnFirstPass = false;

                    goto RunBackupJob_Continue;
                }

                strQry.Clear();

                strQry.AppendLine(" UPDATE InteractPayroll.dbo.BACKUP_DATABASE_PATH");
                strQry.AppendLine(" SET BACKUP_LAST_DATETIME = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'");

                clsDBConnectionObjects.Execute_SQLCommand(strQry.ToString(), -1);

                blnBackupCompletedSuccessful = true;
            }
            catch (Exception ex)
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(pvtstrLogFileDirectory + "DailyDBBackup_Error.txt", true))
                {
                    file.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Error = " + ex.Message);
                }
            }
            finally
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(pvtstrLogFileDirectory + "DailyDBBackup_Info.txt", true))
                {
                    file.WriteLine("RunBackupJob Setting Timer ... " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }

                //Set Next Time for Timer to Fire
                double tickTime = Calculate_Next_Time_Timer_Fires(blnBackupCompletedSuccessful);
                tmrTimer.Interval = tickTime;
                tmrTimer.Start();

                if (client != null)
                {
                    client.Dispose();
                    client = null;
                }

                if (blnBackupCompletedSuccessful == true ||
                    DateTime.Now.Hour == 7)
                {
                    var smtp = new SmtpClient();

                    try
                    {
                        //Email
                        var fromAddress = new MailAddress(strEmail, "DB Backup Server");
                        var toAddress   = new MailAddress(strEmail, "Errol Le Roux");

                        string subject = "Daily DB Backups - " + DateTime.Now.ToString("dd MMMM yyyy");
                        string body    = "Backup Successful";

                        if (blnBackupCompletedSuccessful == false)
                        {
                            body = "Backup UNSUCCESSFUL";
                        }

                        smtp.Host                  = "smtp.gmail.com";
                        smtp.Port                  = 587;
                        smtp.EnableSsl             = true;
                        smtp.DeliveryMethod        = SmtpDeliveryMethod.Network;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials           = new NetworkCredential(fromAddress.Address, strEmailPassword);

                        var message = new MailMessage(fromAddress, toAddress);

                        message.Subject = subject;
                        message.Body    = body;

                        smtp.Send(message);

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(pvtstrLogFileDirectory + "DailyDBBackup_Info.txt", true))
                        {
                            file.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Email Sent Successfully");
                        }
                    }
                    catch (Exception ex)
                    {
                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(pvtstrLogFileDirectory + "DailyDBBackup_Error.txt", true))
                        {
                            file.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Email Error = " + ex.Message);
                        }
                    }
                    finally
                    {
                        smtp.Dispose();
                        smtp = null;
                    }
                }
            }
        }