使用AWSSDK .NET /使用.net中的AWS快照创建新快照并删除旧快照

时间:2019-12-18 20:00:00

标签: c# asp.net .net snapshot aws-sdk-net

我找不到任何有关如何创建或删除快照的好的文档。所以这就是我所做的。因此,这是创建新快照并删除旧快照的解决方案

从Visual Studio中的nuget Manager添加AWSSDK.Core / AWSSDK.EC2 / AWSSDK.S3库。

有关创建快照的更多信息,请参见:https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/MEC2CreateSnapshotCreateSnapshotRequest.html

有关描述快照的更多信息,请参阅: https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/TDescribeSnapshotsRequest.html

有关描述快照的更多信息,请参阅: https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/EC2/TDescribeSnapshotsRequest.html

    public class AWSSnapshots
    {


        private string tagKey = ConfigurationManager.AppSettings["tagKey"].ToString();
        private string tagValue = ConfigurationManager.AppSettings["tagValue"].ToString();
        private int intervalInMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["intervalInMinutes"]);


        public AWSSnapshots()
        {
//This is used on EC2 Machine to get the metadata
            KeyValuePair<string, IAMSecurityCredentialMetadata> iamSecurityCredentialMetadata = EC2InstanceMetadata.IAMSecurityCredentials.FirstOrDefault();
            client = new AmazonEC2Client(iamSecurityCredentialMetadata.Value.AccessKeyId, iamSecurityCredentialMetadata.Value.SecretAccessKey, iamSecurityCredentialMetadata.Value.Token, EC2InstanceMetadata.Region);
//Use this in local machines or anywhere when you know your accesskey and secret key.
            //client = new AmazonEC2Client("AccessKeyId", "SecretAccessKey", RegionEndpoint.APSouth1);
        }


        public void CreateSnapshot()
        {
            try
            { 

                CreateSnapshotsRequest createSnapshotsRequest = new CreateSnapshotsRequest();
                createSnapshotsRequest.InstanceSpecification = new InstanceSpecification
                {
//For Local system, use instance id if you know what it is. Below is used on EC2 to get its on Instance id.
                    InstanceId = EC2InstanceMetadata.InstanceId,
                    ExcludeBootVolume = false
                };

                //createSnapshotsRequest.TagSpecifications = new List<TagSpecification>
                //{
                //    new TagSpecification
                //    {
                //        Tags= new List<Tag>{
                //            new Tag
                //            {
                //                Key = tagKey,
                //                Value = tagValue
                //            }
                //        },
                //        ResourceType="snapshot"
                //    }
                //};

                var response = client.CreateSnapshots(createSnapshotsRequest);
                Console.WriteLine(response.HttpStatusCode);
                Console.WriteLine(response.Snapshots);

            }
            catch
            {

            }
        }


        public void DeleteSnapshot()
        {
            try
            {
//This is search as per the filter provided
                var response = client.DescribeSnapshots(new DescribeSnapshotsRequest
                {
                    Filters = new List<Filter> {
                        new Filter
                        {
                            Name = "tag:"+tagKey,
                            Values = new List<string>
                            {
                                tagValue
                            }
                        }
                    },
                });

                List<Snapshot> snapshots = response.Snapshots;
//getting the data only which needs to be deleted
                var requiredSnapshots = snapshots.Where(s => s.StartTime <= DateTime.Now.AddMinutes(intervalInMinutes));

//deleting older one by one
                foreach (var snapshot in requiredSnapshots)
                {
                    var response1 = client.DeleteSnapshot(new DeleteSnapshotRequest
                    {
                        SnapshotId = snapshot.SnapshotId
                    });
                    Console.WriteLine(response1.HttpStatusCode);
                }
            }
            catch
            {

            }
        }
    }

0 个答案:

没有答案