ListBuckets() public method

Returns a list of all buckets owned by the authenticated sender of the request.
public ListBuckets ( ) : ListBucketsResponse
return ListBucketsResponse
	static void Main()
	{
		// Connect to Amazon S3 service with authentication
		BasicAWSCredentials basicCredentials =
			new BasicAWSCredentials("AKIAIIYG27E27PLQ6EWQ", 
			"hr9+5JrS95zA5U9C6OmNji+ZOTR+w3vIXbWr3/td");
		AmazonS3Client s3Client = new AmazonS3Client(basicCredentials);

		// Display all S3 buckets
		ListBucketsResponse buckets = s3Client.ListBuckets();
		foreach (var bucket in buckets.Buckets)
		{
			Console.WriteLine(bucket.BucketName);
		}

		// Display and download the files in the first S3 bucket
		string bucketName = buckets.Buckets[0].BucketName;
		Console.WriteLine("Objects in bucket '{0}':", bucketName);
		ListObjectsResponse objects =
			s3Client.ListObjects(new ListObjectsRequest() { BucketName = bucketName });
		foreach (var s3Object in objects.S3Objects)
		{
			Console.WriteLine("\t{0} ({1})", s3Object.Key, s3Object.Size);
			if (s3Object.Size > 0)
			{
				// We have a file (not a directory) --> download it
				GetObjectResponse objData = s3Client.GetObject(
					new GetObjectRequest() { BucketName = bucketName, Key = s3Object.Key });
				string s3FileName = new FileInfo(s3Object.Key).Name;
				SaveStreamToFile(objData.ResponseStream, s3FileName);
			}
		}

		// Create a new directory and upload a file in it
		string path = "uploads/new_folder_" + DateTime.Now.Ticks;
		string newFileName = "example.txt";
		string fullFileName = path + "/" + newFileName;
		string fileContents = "This is an example file created through the Amazon S3 API.";
		s3Client.PutObject(new PutObjectRequest() { 
			BucketName = bucketName, 
			Key = fullFileName,
			ContentBody = fileContents}
		);
		Console.WriteLine("Created a file in Amazon S3: {0}", fullFileName);

		// Share the uploaded file and get a download URL
		string uploadedFileUrl = s3Client.GetPreSignedURL(new GetPreSignedUrlRequest()
		{ 
			BucketName = bucketName,
			Key = fullFileName,
			Expires = DateTime.Now.AddYears(5)
		});
		Console.WriteLine("File download URL: {0}", uploadedFileUrl);
		System.Diagnostics.Process.Start(uploadedFileUrl);
	}
Ejemplo n.º 2
0
        public void TestClientDispose()
        {
            IAmazonS3 client;
            using(client = new AmazonS3Client())
            {
                var response = client.ListBuckets();
                Assert.IsNotNull(response);
                Assert.IsNotNull(response.ResponseMetadata);
                Assert.IsNotNull(response.ResponseMetadata.RequestId);
                Assert.IsFalse(string.IsNullOrEmpty(response.ResponseMetadata.RequestId));
            }

            AssertExtensions.ExpectException(() => client.ListBuckets(), typeof(ObjectDisposedException));
        }
Ejemplo n.º 3
0
 private static void List(AmazonS3Client client)
 {
     // Issue call
         ListBucketsResponse listResponse = client.ListBuckets();
     // View response data
     Console.WriteLine("Buckets owner - {0}", listResponse.Owner.DisplayName);
     foreach (S3Bucket bucket in listResponse.Buckets)
     {
         Console.WriteLine("Bucket {0}, Created on {1}", bucket.BucketName, bucket.CreationDate);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Uses the AWS SDK for .NET to talk to Tier 3 Object Storage
        /// </summary>
        private static void UseAwsSdk()
        {
            Console.WriteLine(":: Calling Tier 3 Object Storage from AWS SDK for .NET ::");
            Console.WriteLine();

            //create configuration that points to different URL
            AmazonS3Config config = new AmazonS3Config()
            {
                ServiceURL = "ca.tier3.io"
            };

            AmazonS3Client client = new AmazonS3Client(adminAccessKey, adminAccessSecret, config);

            /*
             * List buckets
             */
            Console.WriteLine("ACTION: List all the buckets");
            ListBucketsResponse resp = client.ListBuckets();

            foreach (S3Bucket bucket in resp.Buckets)
            {
                Console.WriteLine("-" + bucket.BucketName);
            }

            Console.WriteLine();

            /*
             * List objects in a single bucket
             */
            Console.WriteLine("ACTION: Enter the name of a bucket to open: ");
            string inputbucket = Console.ReadLine();

            ListObjectsRequest objReq = new ListObjectsRequest() { BucketName = inputbucket };
            ListObjectsResponse objResp = client.ListObjects(objReq);

            foreach (S3Object obj in objResp.S3Objects)
            {
                Console.WriteLine("-" + obj.Key);
            }

            /*
             * Upload object to bucket
             */
            //Console.Write("Type [Enter] to upload an object to the opened bucket");
            //Console.ReadLine();

            //PutObjectRequest putReq = new PutObjectRequest() { BucketName = inputbucket, FilePath = @"C:\image.png", ContentType = "image/png" };
            //PutObjectResponse putResp = client.PutObject(putReq);

            //Console.WriteLine("Object uploaded.");
            Console.ReadLine();
        }
Ejemplo n.º 5
0
        public override void ActivateOptions()
        {
            var client = new AmazonS3Client();
            ListBucketsResponse response = client.ListBuckets();
            bool found = response.Buckets.Any(bucket => bucket.BucketName == BucketName);

            if (found == false)
            {
                client.PutBucket(new PutBucketRequest().WithBucketName(BucketName));
            }

            base.ActivateOptions();
        }
Ejemplo n.º 6
0
        public static void DeleteAllBuckets()
        {
            using (var client = new AmazonS3Client(Settings.AccessKey, Settings.Secret))
            {
                var response = client.ListBuckets();

                foreach (var bucket in response.Buckets)
                {
                    DeleteAllBucketItems(bucket.BucketName);

                    client.DeleteBucket(bucket.BucketName);
                }
            }
        }
Ejemplo n.º 7
0
        public void TestSessionCredentials()
        {
            using (var sts = new Amazon.SecurityToken.AmazonSecurityTokenServiceClient())
            {
                AWSCredentials credentials = sts.GetSessionToken().Credentials;

                var originalS3Signature = AWSConfigsS3.UseSignatureVersion4;
                AWSConfigsS3.UseSignatureVersion4 = true;
                try
                {
                    using (var ec2 = new Amazon.EC2.AmazonEC2Client(credentials))
                    {
                        var regions = ec2.DescribeRegions().Regions;
                        Console.WriteLine(regions.Count);
                    }

                    using (var s3 = new Amazon.S3.AmazonS3Client(credentials))
                    {
                        var buckets = s3.ListBuckets().Buckets;
                        Console.WriteLine(buckets.Count);
                    }

                    using (var swf = new Amazon.SimpleWorkflow.AmazonSimpleWorkflowClient(credentials))
                    {
                        var domains = swf.ListDomains(new Amazon.SimpleWorkflow.Model.ListDomainsRequest {
                            RegistrationStatus = "REGISTERED"
                        }).DomainInfos;
                        Console.WriteLine(domains.Infos.Count);
                    }


                    using (var swf = new Amazon.SimpleWorkflow.AmazonSimpleWorkflowClient(credentials, new Amazon.SimpleWorkflow.AmazonSimpleWorkflowConfig {
                        SignatureVersion = "4"
                    }))
                    {
                        var domains = swf.ListDomains(new Amazon.SimpleWorkflow.Model.ListDomainsRequest {
                            RegistrationStatus = "REGISTERED"
                        }).DomainInfos;
                        Console.WriteLine(domains.Infos.Count);
                    }
                }
                finally
                {
                    AWSConfigsS3.UseSignatureVersion4 = originalS3Signature;
                }
            }
        }
Ejemplo n.º 8
0
 private static void TestCredentials(ProxyRefreshingAWSCredentials creds, bool expectFailure)
 {
     using (var client = new AmazonS3Client(creds))
     {
         try
         {
             client.ListBuckets();
             Assert.IsFalse(expectFailure);
         }
         catch (AmazonClientException ace)
         {
             Assert.IsTrue(expectFailure);
             Assert.IsNotNull(ace);
             Assert.IsNotNull(ace.Message);
             Assert.IsTrue(ace.Message.IndexOf("already") >= 0);
         }
     }
 }
Ejemplo n.º 9
0
        private void executeSomeBucketOperations(AmazonS3Config s3Config)
        {
            using (var s3Client = new AmazonS3Client(s3Config))
            {
                // Call ListBuckets first to verify that AmazonS3PostMarshallHandler.ProcessRequestHandlers
                // correctly computes the endpoint when no bucket name is present.
                var listBucketsResponse = s3Client.ListBuckets();
                Assert.IsNotNull(listBucketsResponse);
                Assert.IsFalse(string.IsNullOrEmpty(listBucketsResponse.ResponseMetadata.RequestId));

                // Bonus call on ListObjects if we can find a bucket compatible with the test region (to avoid 301
                // errors due to addressing bucket on wrong endpoint). This verifies that  
                // AmazonS3PostMarshallHandler.ProcessRequestHandlers correctly computes the endpoint when 
                // a bucket name is present.
                string bucketName = null;
                foreach (var bucket in listBucketsResponse.Buckets)
                {
                    try
                    {
                        var bucketLocationResponse = s3Client.GetBucketLocation(bucket.BucketName);
                        if (string.IsNullOrEmpty(bucketLocationResponse.Location) && s3Config.RegionEndpoint == RegionEndpoint.USEast1)
                            bucketName = bucket.BucketName;
                        else if (string.Equals(s3Config.RegionEndpoint.SystemName, bucketLocationResponse.Location, StringComparison.OrdinalIgnoreCase))
                            bucketName = bucket.BucketName;

                        if (!string.IsNullOrEmpty(bucketName))
                            break;
                    }
                    catch(AmazonS3Exception e)
                    {
                        if (e.StatusCode != System.Net.HttpStatusCode.NotFound)
                            throw;
                    }
                }

                if (!string.IsNullOrEmpty(bucketName))
                {
                    var listObjectsResponse = s3Client.ListObjects(new ListObjectsRequest { BucketName = bucketName });
                    Assert.IsNotNull(listObjectsResponse);
                    Assert.IsNotNull(listObjectsResponse.ResponseMetadata);
                }
            }
        }
Ejemplo n.º 10
0
        public static void Main()
        {
            Debug.WriteLine("============================================");
            Debug.WriteLine("Welcome to the AWS .NET SDK! Ready, Set, Go!");
            Debug.WriteLine("============================================");

            //The Amazon S3 client allows you to manage buckets and objects programmatically.
            IAmazonS3 s3Client = new AmazonS3Client();

            try
            {
                ListBucketsResponse response = s3Client.ListBuckets();
                int numBuckets = 0;
                if (response.Buckets != null && response.Buckets.Count > 0)
                {
                    numBuckets = response.Buckets.Count;
                }
                Debug.WriteLine("You have " + numBuckets + " Amazon S3 bucket(s)");
            }
            catch (Amazon.S3.AmazonS3Exception S3Exception)
            {
                //AmazonServiceException represents an error response from an AWS service.
                //AWS service received the request but either found it invalid or encountered an error trying to execute it.
                if (S3Exception.ErrorCode != null && (S3Exception.ErrorCode.Equals("InvalidAccessKeyId") || S3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Debug.WriteLine("Please check the provided AWS Credentials.");
                    Debug.Write("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
                    Debug.WriteLine(S3Exception.Message, S3Exception.InnerException);
                }
                else
                {
                    Debug.WriteLine("Error Message:    " + S3Exception.Message);
                    Debug.WriteLine("HTTP Status Code: " + S3Exception.StatusCode);
                    Debug.WriteLine("AWS Error Code:   " + S3Exception.ErrorCode);
                    Debug.WriteLine("Request ID:       " + S3Exception.RequestId);
                }
            }
            finally
            {
                s3Client.Dispose();
            };
        }
Ejemplo n.º 11
0
        public void TestSessionCredentials()
        {
            using (var sts = new Amazon.SecurityToken.AmazonSecurityTokenServiceClient())
            {
                AWSCredentials credentials = sts.GetSessionToken().Credentials;

                var originalS3Signature = AWSConfigsS3.UseSignatureVersion4;
                AWSConfigsS3.UseSignatureVersion4 = true;
                try
                {

                    using (var ec2 = new Amazon.EC2.AmazonEC2Client(credentials))
                    {
                        var regions = ec2.DescribeRegions().Regions;
                        Console.WriteLine(regions.Count);
                    }

                    using (var s3 = new Amazon.S3.AmazonS3Client(credentials))
                    {
                        var buckets = s3.ListBuckets().Buckets;
                        Console.WriteLine(buckets.Count);
                    }

                    using (var swf = new Amazon.SimpleWorkflow.AmazonSimpleWorkflowClient(credentials))
                    {
                        var domains = swf.ListDomains(new Amazon.SimpleWorkflow.Model.ListDomainsRequest { RegistrationStatus = "REGISTERED" }).DomainInfos;
                        Console.WriteLine(domains.Infos.Count);
                    }

                    using (var swf = new Amazon.SimpleWorkflow.AmazonSimpleWorkflowClient(credentials, new Amazon.SimpleWorkflow.AmazonSimpleWorkflowConfig { SignatureVersion = "4" }))
                    {
                        var domains = swf.ListDomains(new Amazon.SimpleWorkflow.Model.ListDomainsRequest { RegistrationStatus = "REGISTERED" }).DomainInfos;
                        Console.WriteLine(domains.Infos.Count);
                    }
                }
                finally
                {
                    AWSConfigsS3.UseSignatureVersion4 = originalS3Signature;
                }
            }
        }
Ejemplo n.º 12
0
        public void TestExplicitDualstackEndpoint()
        {
            var config = new AmazonS3Config
            {
                ServiceURL = "https://s3.dualstack.us-west-2.amazonaws.com"
            };

            using (var s3Client = new AmazonS3Client(config))
            {
                var listBucketsResponse = s3Client.ListBuckets();
                Assert.IsNotNull(listBucketsResponse);
                Assert.IsFalse(string.IsNullOrEmpty(listBucketsResponse.ResponseMetadata.RequestId));
            }
        }
Ejemplo n.º 13
0
        public void TestHttpAccessOnDualstackEndpoint()
        {
            var config = new AmazonS3Config
            {
                UseDualstackEndpoint = true,
                RegionEndpoint = RegionEndpoint.USWest2,
                UseHttp = true
            };

            using (var s3Client = new AmazonS3Client(config))
            {
                var listBucketsResponse = s3Client.ListBuckets();
                Assert.IsNotNull(listBucketsResponse);
                Assert.IsFalse(string.IsNullOrEmpty(listBucketsResponse.ResponseMetadata.RequestId));
            }
        }
Ejemplo n.º 14
0
        /*
         Sample call for upload:-
            byte[] array = new byte[1024*1024*1024];
            Random random = new Random();
            random.NextBytes(array);
            double timeTaken_Upload = Experiment.doRawCloudPerf(array, SynchronizerType.Azure, SynchronizeDirection.Upload, "fooContainer", "fooBlob");
            double timeTaken_Download = Experiment.doRawCloudPerf(array, SynchronizerType.Azure, SynchronizeDirection.Download, "fooContainer", "fooBlob");
         *
         *
         */
        public static double doRawCloudPerf(byte[] input, SynchronizerType synchronizerType, 
            SynchronizeDirection syncDirection, string exp_directory, Logger logger, string containerName=null, string blobName=null)
        {
            string accountName = ConfigurationManager.AppSettings.Get("AccountName");
            string accountKey = ConfigurationManager.AppSettings.Get("AccountSharedKey");

            DateTime begin=DateTime.Now, end=DateTime.Now;

            if (synchronizerType == SynchronizerType.Azure)
            {
                #region azure download/upload
                if (containerName==null)
                    containerName = "testingraw";
                if(blobName==null)
                    blobName = Guid.NewGuid().ToString();

                CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(accountName, accountKey), true);
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference(containerName);

                if (syncDirection == SynchronizeDirection.Upload)
                {
                    logger.Log("Start Stream Append");
                    container.CreateIfNotExist();
                    begin = DateTime.UtcNow;//////////////////////////////////////
                    try
                    {
                        using (MemoryStream memoryStream = new System.IO.MemoryStream(input))
                        {
                            CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
                            blockBlob.UploadFromStream(memoryStream);
                        }
                    }
                    catch (Exception e)
                    {
                    }
                    end = DateTime.UtcNow;//////////////////////////////////////
                    logger.Log("End Stream Append");
                }

                if (syncDirection == SynchronizeDirection.Download)
                {
                    logger.Log("Start Stream Get");
                    logger.Log("Start Stream GetAll");
                    try
                    {
                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
                        byte[] blobContents = blockBlob.DownloadByteArray();

                        //if (File.Exists(blobName))
                        //   File.Delete(blobName);

                        begin = DateTime.UtcNow;//////////////////////////////////////
                        // using (FileStream fs = new FileStream(blobName, FileMode.OpenOrCreate))
                        // {
                            byte[] contents = blockBlob.DownloadByteArray();
                            // fs.Write(contents, 0, contents.Length);
                        // }
                    }
                    catch (Exception e)
                    {
                    }
                    end = DateTime.UtcNow;//////////////////////////////////////
                    logger.Log("End Stream Get");
                    logger.Log("End Stream GetAll");

                }
                #endregion

            }

            else if (synchronizerType == SynchronizerType.AmazonS3)
            {
                #region amazon s3 stuff
                if (containerName == null)
                    containerName = "testingraw";
                if (blobName == null)
                    blobName = Guid.NewGuid().ToString();

                AmazonS3Client amazonS3Client = new AmazonS3Client(accountName, accountKey);
                if (syncDirection == SynchronizeDirection.Upload)
                {
                    ListBucketsResponse response = amazonS3Client.ListBuckets();
                    foreach (S3Bucket bucket in response.Buckets)
                    {
                        if (bucket.BucketName == containerName)
                        {
                            break;
                        }
                    }
                    amazonS3Client.PutBucket(new PutBucketRequest().WithBucketName(containerName));

                    begin = DateTime.UtcNow;//////////////////////////////////////
                    MemoryStream ms = new MemoryStream();
                    ms.Write(input, 0, input.Length);
                    PutObjectRequest request = new PutObjectRequest();
                    request.WithBucketName(containerName);
                    request.WithKey(blobName);
                    request.InputStream = ms;
                    amazonS3Client.PutObject(request);
                    end = DateTime.UtcNow;//////////////////////////////////////

                }

                if (syncDirection == SynchronizeDirection.Download)
                {
                    if (File.Exists(blobName))
                        File.Delete(blobName);

                    begin = DateTime.UtcNow;//////////////////////////////////////
                    GetObjectRequest request = new GetObjectRequest();
                    request.WithBucketName(containerName);
                    request.WithKey(blobName);
                    GetObjectResponse response = amazonS3Client.GetObject(request);
                    var localFileStream = File.Create(blobName);
                    response.ResponseStream.CopyTo(localFileStream);
                    localFileStream.Close();
                    end = DateTime.UtcNow;//////////////////////////////////////
                }
            #endregion
            }
            else
            {
                throw new InvalidDataException("syncronizer type is not valid");
            }

            return (end - begin).TotalMilliseconds;// return total time to upload in milliseconds
        }
Ejemplo n.º 15
0
        public void BucketSamples()
        {
            {
                #region ListBuckets Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Issue call
                ListBucketsResponse response = client.ListBuckets();

                // View response data
                Console.WriteLine("Buckets owner - {0}", response.Owner.DisplayName);
                foreach (S3Bucket bucket in response.Buckets)
                {
                    Console.WriteLine("Bucket {0}, Created on {1}", bucket.BucketName, bucket.CreationDate);
                }

                #endregion
            }

            {
                #region BucketPolicy Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Put sample bucket policy (overwrite an existing policy)
                string newPolicy = @"{ 
    ""Statement"":[{ 
    ""Sid"":""BasicPerms"", 
    ""Effect"":""Allow"", 
    ""Principal"": ""*"", 
    ""Action"":[""s3:PutObject"",""s3:GetObject""], 
    ""Resource"":[""arn:aws:s3:::samplebucketname/*""] 
}]}";
                PutBucketPolicyRequest putRequest = new PutBucketPolicyRequest
                {
                    BucketName = "SampleBucket",
                    Policy = newPolicy
                };
                client.PutBucketPolicy(putRequest);


                // Retrieve current policy
                GetBucketPolicyRequest getRequest = new GetBucketPolicyRequest
                {
                    BucketName = "SampleBucket"
                };
                string policy = client.GetBucketPolicy(getRequest).Policy;

                Console.WriteLine(policy);
                Debug.Assert(policy.Contains("BasicPerms"));


                // Delete current policy
                DeleteBucketPolicyRequest deleteRequest = new DeleteBucketPolicyRequest
                {
                    BucketName = "SampleBucket"
                };
                client.DeleteBucketPolicy(deleteRequest);


                // Retrieve current policy and verify that it is null
                policy = client.GetBucketPolicy(getRequest).Policy;
                Debug.Assert(policy == null);

                #endregion
            }

            {
                #region GetBucketLocation Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Construct request
                GetBucketLocationRequest request = new GetBucketLocationRequest
                {
                    BucketName = "SampleBucket"
                };

                // Issue call
                GetBucketLocationResponse response = client.GetBucketLocation(request);

                // View response data
                Console.WriteLine("Bucket location - {0}", response.Location);

                #endregion
            }

            {
                #region PutBucket Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Construct request
                PutBucketRequest request = new PutBucketRequest
                {
                    BucketName = "SampleBucket",
                    BucketRegion = S3Region.EU,         // set region to EU
                    CannedACL = S3CannedACL.PublicRead  // make bucket publicly readable
                };

                // Issue call
                PutBucketResponse response = client.PutBucket(request);

                #endregion
            }

            {
                #region DeleteBucket Sample 1

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Construct request
                DeleteBucketRequest request = new DeleteBucketRequest
                {
                    BucketName = "SampleBucket"
                };

                // Issue call
                DeleteBucketResponse response = client.DeleteBucket(request);

                #endregion
            }

            {
                #region DeleteBucket Sample 2

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // List and delete all objects
                ListObjectsRequest listRequest = new ListObjectsRequest
                {
                    BucketName = "SampleBucket"
                };

                ListObjectsResponse listResponse;
                do
                {
                    // Get a list of objects
                    listResponse = client.ListObjects(listRequest);
                    foreach (S3Object obj in listResponse.S3Objects)
                    {
                        // Delete each object
                        client.DeleteObject(new DeleteObjectRequest
                        {
                            BucketName = "SampleBucket",
                            Key = obj.Key
                        });
                    }

                    // Set the marker property
                    listRequest.Marker = listResponse.NextMarker;
                } while (listResponse.IsTruncated);

                // Construct DeleteBucket request
                DeleteBucketRequest request = new DeleteBucketRequest
                {
                    BucketName = "SampleBucket"
                };

                // Issue call
                DeleteBucketResponse response = client.DeleteBucket(request);

                #endregion
            }

            {
                #region LifecycleConfiguration Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();


                // Put sample lifecycle configuration (overwrite an existing configuration)
                LifecycleConfiguration newConfiguration = new LifecycleConfiguration
                {
                    Rules = new List<LifecycleRule>
                    {
                        // Rule to delete keys with prefix "Test-" after 5 days
                        new LifecycleRule
                        {
                            Prefix = "Test-",
                            Expiration = new LifecycleRuleExpiration { Days = 5 }
                        },
                        // Rule to delete keys in subdirectory "Logs" after 2 days
                        new LifecycleRule
                        {
                            Prefix = "Logs/",
                            Expiration = new LifecycleRuleExpiration  { Days = 2 },
                            Id = "log-file-removal"
                        }
                    }
                };
                PutLifecycleConfigurationRequest putRequest = new PutLifecycleConfigurationRequest
                {
                    BucketName = "SampleBucket",
                    Configuration = newConfiguration
                };
                client.PutLifecycleConfiguration(putRequest);


                // Retrieve current configuration
                GetLifecycleConfigurationRequest getRequest = new GetLifecycleConfigurationRequest
                {
                    BucketName = "SampleBucket"
                };
                LifecycleConfiguration configuration = client.GetLifecycleConfiguration(getRequest).Configuration;

                Console.WriteLine("Configuration contains {0} rules", configuration.Rules.Count);
                foreach (LifecycleRule rule in configuration.Rules)
                {
                    Console.WriteLine("Rule");
                    Console.WriteLine(" Prefix = " + rule.Prefix);
                    Console.WriteLine(" Expiration (days) = " + rule.Expiration.Days);
                    Console.WriteLine(" Id = " + rule.Id);
                    Console.WriteLine(" Status = " + rule.Status);
                }


                // Put a new configuration and overwrite the existing configuration
                configuration.Rules.RemoveAt(0);    // remove first rule
                client.PutLifecycleConfiguration(putRequest);

                // Delete current configuration
                DeleteLifecycleConfigurationRequest deleteRequest = new DeleteLifecycleConfigurationRequest
                {
                    BucketName = "SampleBucket"
                };
                client.DeleteLifecycleConfiguration(deleteRequest);


                // Retrieve current configuration and verify that it is null
                configuration = client.GetLifecycleConfiguration(getRequest).Configuration;
                Debug.Assert(configuration == null);

                #endregion
            }
        }
Ejemplo n.º 16
0
        public string Push(AmazonS3Client s3Client, AmazonCodeDeployClient codeDeployClient)
        {
            var zipFileName = string.Format("{0}.{1}.{2}.zip", ApplicationSetName, Version, BundleName);
            var tempPath = Path.Combine(Path.GetTempPath(), zipFileName + "." + Guid.NewGuid() + ".zip");

            ZipFile.CreateFromDirectory(_bundleDirectory.FullName, tempPath, CompressionLevel.Optimal, false, Encoding.ASCII);

            var allTheBuckets = s3Client.ListBuckets(new ListBucketsRequest()).Buckets;

            if (!allTheBuckets.Exists(b => b.BucketName == Bucket))
            {
                s3Client.PutBucket(new PutBucketRequest { BucketName = Bucket, UseClientRegion = true });
            }

            var putResponse = s3Client.PutObject(new PutObjectRequest
            {
                BucketName = Bucket,
                Key = zipFileName,
                FilePath = tempPath,
            });

            var registration = new RegisterApplicationRevisionRequest
            {
                ApplicationName = CodeDeployApplicationName,
                Description = "Revision " + Version,
                Revision = new RevisionLocation
                {
                    RevisionType = RevisionLocationType.S3,
                    S3Location = new S3Location
                    {
                        Bucket = Bucket,
                        BundleType = BundleType.Zip,
                        Key = zipFileName,
                        Version = Version
                    }
                }
            };
            try
            {
                codeDeployClient.RegisterApplicationRevision(registration);
            }
            catch (ApplicationDoesNotExistException)
            {
                codeDeployClient.CreateApplication(new CreateApplicationRequest { ApplicationName = CodeDeployApplicationName });
                codeDeployClient.RegisterApplicationRevision(registration);
            }

            return putResponse.ETag;
        }
Ejemplo n.º 17
0
        private static void Initialize()
        {
            NameValueCollection appConfig = ConfigurationManager.AppSettings;
            _amazonS3Client = new AmazonS3Client(appConfig["AWSAccessKey"], appConfig["AWSSecretKey"]);

            _folder = appConfig["SyncFolder"];

            string password = appConfig["CryptoKey"];
            Rfc2898DeriveBytes rfcDb = new Rfc2898DeriveBytes(password, System.Text.Encoding.UTF8.GetBytes(password));

            Rijndael rijndael = Rijndael.Create();

            byte[] key = rfcDb.GetBytes(32); //256 bits key
            byte[] iv = rfcDb.GetBytes(16); // 128 bits key
            _aesEncryptor = rijndael.CreateEncryptor(key, iv);
            _aesDecryptor = rijndael.CreateDecryptor(key, iv);

            ListBucketsResponse listBucketsResponse = _amazonS3Client.ListBuckets();
            bool bucketIsExist = listBucketsResponse.Buckets.Any(s3Bucket => s3Bucket.BucketName == BucketName);

            if (!bucketIsExist)
            {
                _amazonS3Client.PutBucket(new PutBucketRequest().WithBucketName(BucketName));
            }

            InitializeFileSystemWatcher();
        }
Ejemplo n.º 18
0
 public void NoCredentialsOnContext()
 {
     var request = new AssumeRoleRequest()
     {
         DurationSeconds = 0 // invalid value for DurationSeconds - credentials will be null when retrying request
     };
     var credentials = new STSAssumeRoleAWSCredentials(new AmazonSecurityTokenServiceClient(), request);
     var s3Client = new AmazonS3Client(credentials);
     AssertExtensions.ExpectException(() => { s3Client.ListBuckets(); }, typeof(AmazonSecurityTokenServiceException), new Regex("3 validation errors detected"));
 }
Ejemplo n.º 19
0
        public static S3Bucket FindBucketByName(string name)
        {
            using (var client = new AmazonS3Client(Settings.AccessKey, Settings.Secret))
            {
                var response = client.ListBuckets();

                foreach (var bucket in response.Buckets.Where(x => x.BucketName == name))
                {
                    return bucket;
                }
            }

            return null;
        }
Ejemplo n.º 20
0
        private void LoadBuckets(Amazon.RegionEndpoint region)
        {
            S3Client = new AmazonS3Client(region);

            var response = S3Client.ListBuckets();

            foreach (var bucket in response.Buckets)
            {
                regionLoadWorker.ReportProgress(1, bucket);
            }
        }
Ejemplo n.º 21
0
        public DataTable GetS3Buckets(string aprofile,string Region2Scan)
        {
            string accountid = GetAccountID(aprofile);
            Amazon.Runtime.AWSCredentials credential;
            DataTable ToReturn = AWSTables.GetComponentTable("S3");
            RegionEndpoint Endpoint2scan = RegionEndpoint.USEast1;
            //Convert the Region2Scan to an AWS Endpoint.
            foreach (var aregion in RegionEndpoint.EnumerableAllRegions)
            {
                if (aregion.DisplayName.Equals(Region2Scan))
                {
                    Endpoint2scan = aregion;
                    continue;
                }
            }


            //Query Cloudwatch to get list of buckets and sizes in this here region
            var Sizetable = S3SizeCloudWatch(aprofile, Region2Scan);
            int sizerows = Sizetable.Rows.Count;



            try
            {
                credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile);


                
                
                AmazonS3Client S3Client = new AmazonS3Client(credential, Endpoint2scan);
                ListBucketsResponse response = S3Client.ListBuckets();

                //Why are we getting USEast buckets?  Because the Cloudwatch returns buckets by region, but list buckets does care about regions!


                foreach (S3Bucket abucket in response.Buckets)
                {
                    DataRow abucketrow = ToReturn.NewRow();
                    var name = abucket.BucketName;
                    DataRow bucketsizedata = AWSTables.GetS3SizesTable().NewRow();
                    Boolean havesize = true;

                    try//Lookup the record in the Sizetable for this bucket.
                    {
                        //This is equivalent to the LINQ query.
                        Boolean foundinsizetable = false;
                        List<string> bn = new List<string>();
                        foreach(var rabbit in Sizetable.AsEnumerable())
                        {
                            String thisun = rabbit["Bucket"].ToString();
                            bn.Add(thisun);
                            if (thisun.Equals(name))
                            {
                                bucketsizedata = rabbit;
                                foundinsizetable = true;
                            }
                        }
                        if(!foundinsizetable)
                        {
                            bn.Sort();
                            if (bn.Contains(name))
                            {
                                string rabbit = "Yes it does!";
                            }
                            //why not?
                        }



                        if (bucketsizedata == null)
                        {
                            abucketrow["EndDate"] = AWSTables.Shrug;
                            havesize = false;
                        }

                    }
                    catch
                    {

                    }


                    try
                    {
                        GetBucketLocationRequest gbr = new GetBucketLocationRequest();
                        gbr.BucketName = name;
                        GetBucketLocationResponse location = S3Client.GetBucketLocation(gbr);

                        var region = location.Location.Value;

                        if (region.Equals(""))
                        {
                            
                            region = "us-east-1";
                        }

                        if (!region.Contains(Endpoint2scan.SystemName))
                        {
                            name = name;//Bucketname
                            region = region;//BucketRegion
                            string target = Endpoint2scan.SystemName;
                            continue;
                        }
                        var pointy = RegionEndpoint.GetBySystemName(region);

                        //Build a config that references the buckets region.
                        AmazonS3Config S3C = new AmazonS3Config();
                        S3C.RegionEndpoint = pointy;
                        AmazonS3Client BS3Client = new AmazonS3Client(credential, S3C);

                        var authregion = "";
                        var EP = BS3Client.Config.RegionEndpoint.DisplayName;
                        if (String.IsNullOrEmpty(BS3Client.Config.RegionEndpoint.DisplayName)) authregion = "";
                        else
                        {
                            authregion = BS3Client.Config.AuthenticationRegion;
                        }

                        string authservice = "";

                        if (string.IsNullOrEmpty(BS3Client.Config.AuthenticationServiceName)) authservice = "";
                        else
                        {
                            authservice = BS3Client.Config.AuthenticationServiceName;
                        }

                        var createddate = abucket.CreationDate;
                        string owner = "";
                        string grants = "";
                        string tags = "";
                        string lastaccess = "";
                        string defaultpage = "";
                        string website = "";
                        //Now start pulling der einen data.

                        GetACLRequest GACR = new GetACLRequest();
                        GACR.BucketName = name;
                        var ACL = BS3Client.GetACL(GACR);
                        var grantlist = ACL.AccessControlList;
                        owner = grantlist.Owner.DisplayName;
                        foreach (var agrant in grantlist.Grants)
                        {
                            if (grants.Length > 1) grants += "\n";
                            var gName = agrant.Grantee.DisplayName;
                            var gType = agrant.Grantee.Type.Value;
                            var aMail = agrant.Grantee.EmailAddress;

                            if (gType.Equals("Group"))
                            {
                                grants += gType + " - " + agrant.Grantee.URI + " - " + agrant.Permission + " - " + aMail;
                            }
                            else
                            {
                                grants += gName + " - " + agrant.Permission + " - " + aMail;
                            }
                        }

                        GetBucketWebsiteRequest GBWReq = new GetBucketWebsiteRequest();
                        GBWReq.BucketName = name;
                        GetBucketWebsiteResponse GBWRes = BS3Client.GetBucketWebsite(GBWReq);

                        defaultpage = GBWRes.WebsiteConfiguration.IndexDocumentSuffix;


                        if (defaultpage != null)
                        {
                            website = @"http://" + name + @".s3-website-" + region + @".amazonaws.com/" + defaultpage;
                        }
                        abucketrow["AccountID"] = accountid;
                        abucketrow["Profile"] = aprofile;
                        abucketrow["Bucket"] = name;
                        abucketrow["Region"] = region;
                        abucketrow["RegionEndpoint"] = EP;
                        abucketrow["AuthRegion"] = authregion;
                        abucketrow["AuthService"] = authservice;

                        abucketrow["CreationDate"] = createddate.ToString();
                        abucketrow["LastAccess"] = lastaccess;
                        abucketrow["Owner"] = owner;
                        abucketrow["Grants"] = grants;
                        abucketrow["StartDate"] = "NA";
                        abucketrow["StartSizeMin"] = "NA";
                        abucketrow["StartSizeAVG"] = "NA";
                        abucketrow["StartSizeMax"] = "NA";

                        abucketrow["EndDate"] = "NA";
                        abucketrow["EndSizeMin"] = "NA";
                        abucketrow["EndSizeAVG"] = "NA";
                        abucketrow["EndSizeMax"] = "NA";
                        abucketrow["EndSizeMaxBytes"] = 0;

                        if (havesize)
                        {
                            abucketrow["StartDate"] = bucketsizedata["StartDate"];
                            abucketrow["StartSizeMin"] = bucketsizedata["StartSizeMin"];
                            abucketrow["StartSizeAVG"] = bucketsizedata["StartSizeAVG"];
                            abucketrow["StartSizeMax"] = bucketsizedata["StartSizeMax"];

                            abucketrow["EndDate"] = bucketsizedata["EndDate"];
                            abucketrow["EndSizeMin"] = bucketsizedata["EndSizeMin"];
                            abucketrow["EndSizeAVG"] = bucketsizedata["EndSizeAVG"];
                            abucketrow["EndSizeMax"] = bucketsizedata["EndSizeMax"];
                            abucketrow["EndSizeMaxBytes"] = bucketsizedata["EndSizeMaxBytes"];
                        }



                        abucketrow["WebsiteHosting"] = website;
                        abucketrow["Logging"] = "X";
                        abucketrow["Events"] = "X";
                        abucketrow["Versioning"] = "X";
                        abucketrow["LifeCycle"] = "X";
                        abucketrow["Replication"] = "X";
                        abucketrow["Tags"] = "X";
                        abucketrow["RequesterPays"] = "X";
                        ToReturn.Rows.Add(abucketrow.ItemArray);
                    }
                    catch (Exception ex)
                    {

                        abucketrow["AccountID"] = accountid;
                        abucketrow["Profile"] = aprofile;
                        abucketrow["Bucket"] = name;
                        abucketrow["Region"] = ex.InnerException.Message;

                        if (havesize)
                        {
                            abucketrow["StartDate"] = bucketsizedata["StartDate"];
                            abucketrow["StartSizeMin"] = bucketsizedata["StartSizeMin"];
                            abucketrow["StartSizeAVG"] = bucketsizedata["StartSizeAVG"];
                            abucketrow["StartSizeMax"] = bucketsizedata["StartSizeMax"];

                            abucketrow["EndDate"] = bucketsizedata["EndDate"];
                            abucketrow["EndSizeMin"] = bucketsizedata["EndSizeMin"];
                            abucketrow["EndSizeAVG"] = bucketsizedata["EndSizeAVG"];
                            abucketrow["EndSizeMax"] = bucketsizedata["EndSizeMax"];
                            abucketrow["EndSizeMaxBytes"] = bucketsizedata["EndSizeMaxBytes"];

                        }
                        else
                        {
                            abucketrow["StartDate"] = "NA";
                            abucketrow["StartSizeMin"] = "NA";
                            abucketrow["StartSizeAVG"] = "NA";
                            abucketrow["StartSizeMax"] = "NA";

                            abucketrow["EndDate"] = "NA";
                            abucketrow["EndSizeMin"] = "NA";
                            abucketrow["EndSizeAVG"] = "NA";
                            abucketrow["EndSizeMax"] = "NA";
                            abucketrow["EndSizeMaxBytes"] = 0;
                        }

                        ToReturn.Rows.Add(abucketrow.ItemArray);
                    }
                }




            }
            catch(Exception ex)
            {
                //Croak
            }
            int sizesreturned = Sizetable.Rows.Count;
            int rowsreturned = ToReturn.Rows.Count;
            return ToReturn;
        }
Ejemplo n.º 22
0
        private void CreateAndCheckTestBucket()
        {
            TestBucketIsReady = false;
            USEast1Client = new AmazonS3Client(RegionEndpoint.USEast1);
            USWest1Client = new AmazonS3Client(RegionEndpoint.USWest1);
            var sessionCredentials = new AmazonSecurityTokenServiceClient().GetSessionToken().Credentials;
            USEast1ClientWithSessionCredentials = new AmazonS3Client(sessionCredentials, RegionEndpoint.USEast1);

            TestBucket = USWest1Client.ListBuckets().Buckets.Find(bucket => bucket.BucketName.StartsWith(BucketPrefix));
            if (TestBucket == null)
            {
                // add ticks to bucket name because the bucket namespace is shared globally
                var bucketName = BucketPrefix + DateTime.Now.Ticks;
                // Create the bucket but don't run the test.
                // If the bucket is ready the next time this test runs we'll test then.
                USWest1Client.PutBucket(new PutBucketRequest()
                {
                    BucketRegion = S3Region.USW1,
                    BucketName = bucketName,
                });
            }
            else if (TestBucket.CreationDate.AddHours(TemporaryRedirectMaxExpirationHours) < DateTime.Now)
            {
                BucketRegionDetector.BucketRegionCache.Clear();
                TestBucketIsReady = true;
            }
        }
        public IAmazonS3 CreateClientFromIAMCredentials(IAMSecurityCredential iamCredentials) {
            Logger.Information("Creating an s3 client using IAM credentials");
            Logger.Information("AccessKeyId:{0}", iamCredentials.AccessKeyId);
            Logger.Information("SecretAccessKey:{0}", iamCredentials.SecretAccessKey);
            Logger.Information("Token:{0}", iamCredentials.Token);
            var client = new AmazonS3Client(iamCredentials.AccessKeyId, iamCredentials.SecretAccessKey, iamCredentials.Token, Amazon.RegionEndpoint.EUWest1);

            ListBucketsRequest request=new ListBucketsRequest();
            var buckets = client.ListBuckets(request);

            Logger.Information("List buckets:");
            foreach (var bucket in buckets.Buckets) {
                Logger.Information("{0}", bucket.BucketName);
            }
            return client;
        }
Ejemplo n.º 24
0
 /// <summary>
 /// get the buckets from s3
 /// </summary>
 /// <param name="Buckets"></param>
 /// <returns></returns>
 public bool GetBuckets(out IEnumerable<string> Buckets)
 {
     Buckets = null;
     try
     {
         //amazon client
         using (var client = new AmazonS3Client())
         {
             //get bucket request
             using (var response = client.ListBuckets(new ListBucketsRequest()))
             {
                 List<string> list = new List<string>();
                 response.Buckets.ForEach(b => list.Add(b.BucketName));
                 Buckets = list;
                 return true;
             }
         }
     }
     catch (Exception ex)
     {
         Trace.WriteLine(ex.ToString());
     }
     return false;
 }
Ejemplo n.º 25
0
        public static IEnumerable<string> FindBuckets()
        {
            using (var client = new AmazonS3Client(Settings.AccessKey, Settings.Secret))
            {
                var response = client.ListBuckets();

                foreach (var bucket in response.Buckets)
                {
                    yield return bucket.BucketName;
                }
            }
        }
Ejemplo n.º 26
0
        public Dictionary<string, DataTable> ScanProfile(ScanRequest Request)
        {
            Dictionary<string, DataTable> ScanResults = new Dictionary<string, DataTable>();
            DataTable UserDetailsTable = GetUsersDetailsTable();
            DataTable EC2DetailsTable = GetEC2DetailsTable();
            DataTable S3DetailsTable = GetS3DetailsTable();
            string accountid = "";
            Amazon.Runtime.AWSCredentials credential;
            var aprofile = Request.Profile;
            var regions2process = Request.Regions;
            var SubmitResults = Request.ResultQueue;
            try
            {
                credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile);
                //Try to get the AccountID ID//

                #region UserDetails
                var iam = new AmazonIdentityManagementServiceClient(credential);

                var myUserList = iam.ListUsers().Users;

                try
                {
                    accountid = myUserList[0].Arn.Split(':')[4];//Get the ARN and extract the AccountID ID
                    accountid = "ID: " + accountid;// Prefix added because Excel exsucks.
                }
                catch
                {
                    accountid = "?";
                }

                try // Send command to AWS to generate a Credential Report
                { var createcredreport = iam.GenerateCredentialReport(); }
                catch (Exception)
                { throw; }

                bool needreport = true;

                Amazon.IdentityManagement.Model.GetCredentialReportResponse credreport = new GetCredentialReportResponse();
                DateTime getreportstart = DateTime.Now;
                DateTime getreportfinish = DateTime.Now;
                while (needreport)
                {
                    try
                    {
                        credreport = iam.GetCredentialReport();
                        needreport = false;
                        getreportfinish = DateTime.Now;
                        var dif = getreportstart - getreportfinish;  //Just a check on how long it takes.

                        //Extract data from CSV Stream into DataTable
                        var streambert = credreport.Content;
                        streambert.Position = 0;
                        StreamReader sr = new StreamReader(streambert);
                        string myStringRow = sr.ReadLine();
                        if (myStringRow != null) myStringRow = sr.ReadLine();//Dump the header line
                        while (myStringRow != null)
                        {
                            var arow = myStringRow.Split(",".ToCharArray()[0]);

                            var newrow = new object[UserDetailsTable.Columns.Count];
                            newrow[0] = accountid;
                            newrow[1] = aprofile;
                            newrow[2] = ""; //UserID not in report. pull it later.
                            newrow[3] = arow[0];
                            newrow[4] = arow[1];
                            newrow[5] = arow[2];
                            newrow[6] = arow[3];
                            newrow[7] = arow[4];
                            newrow[8] = arow[5];
                            newrow[9] = arow[6];
                            newrow[10] = arow[7];
                            newrow[11] = arow[8];
                            newrow[12] = arow[9];
                            newrow[13] = arow[10];
                            newrow[14] = arow[11];
                            newrow[15] = arow[12];
                            newrow[16] = arow[13];
                            newrow[17] = arow[14];
                            newrow[18] = arow[15];
                            newrow[19] = arow[16];
                            newrow[20] = arow[17];
                            newrow[21] = arow[18];
                            newrow[22] = arow[19];
                            newrow[23] = arow[20];
                            newrow[24] = arow[21];
                            RawUsers.Rows.Add(newrow);
                            UserDetailsTable.Rows.Add(newrow);
                            myStringRow = sr.ReadLine();
                        }
                        sr.Close();
                        sr.Dispose();

                    }
                    catch (Exception ex)
                    {
                        string test = "";
                        //Deal with this later if necessary.
                    }
                }

                foreach (var auser in myUserList)//Fill in the userID.  Why?  because it exists.
                {
                    string auserid = auser.UserId;
                    string arn = auser.Arn;
                    string username = auser.UserName;
                    string policylist = "";
                    string aklist = "";
                    string groups = "";

                    ListAccessKeysRequest LAKREQ = new ListAccessKeysRequest();
                    LAKREQ.UserName = username;
                    ListAccessKeysResult LAKRES = iam.ListAccessKeys(LAKREQ);
                    foreach (var blivet in LAKRES.AccessKeyMetadata)
                    {
                        if (aklist.Length > 1) aklist += "\n";
                        aklist += blivet.AccessKeyId + "  :  " + blivet.Status;
                    }

                    ListAttachedUserPoliciesRequest LAUPREQ = new ListAttachedUserPoliciesRequest();
                    LAUPREQ.UserName = username;
                    ListAttachedUserPoliciesResult LAUPRES = iam.ListAttachedUserPolicies(LAUPREQ);
                    foreach (var apol in LAUPRES.AttachedPolicies)
                    {
                        if (policylist.Length > 1) policylist += "\n";
                        policylist += apol.PolicyName;
                    }

                    //Need to get policy and group info outta user
                    var groopsreq = new ListGroupsForUserRequest();
                    groopsreq.UserName = username;
                    ListGroupsForUserResult LG = iam.ListGroupsForUser(groopsreq);
                    foreach (var agroup in LG.Groups)
                    {
                        if (groups.Length > 1) groups += "\n";
                        groups += agroup.GroupName;
                    }

                    foreach (DataRow myrow in UserDetailsTable.Rows)
                    {
                        if (myrow["ARN"].Equals(arn))
                        {
                            myrow["UserID"] = auserid;
                            myrow["User-Policies"] = policylist;
                            myrow["Access-Keys"] = aklist;
                            myrow["Groups"] = groups;
                        }
                    }

                }
                #endregion

                #region S3Details
                try {

                AmazonS3Client S3Client = new AmazonS3Client(credential,Amazon.RegionEndpoint.USEast1);
                ListBucketsResponse response = S3Client.ListBuckets();
                foreach (S3Bucket abucket in response.Buckets)
                {

                    DataRow abucketrow = GetS3DetailsTable().NewRow();
                    var name = abucket.BucketName;

                    GetBucketLocationRequest gbr = new GetBucketLocationRequest();
                    gbr.BucketName=name;
                    GetBucketLocationResponse location = S3Client.GetBucketLocation(gbr);
                    var region = location.Location.Value;
                    if (region.Equals(""))region="us-east-1";
                    var pointy = RegionEndpoint.GetBySystemName(region);

                    //Build a config that references the buckets region.
                    AmazonS3Config S3C = new AmazonS3Config();
                    S3C.RegionEndpoint=pointy;
                    AmazonS3Client BS3Client = new AmazonS3Client(credential, S3C);

                    var createddate = abucket.CreationDate;
                    string owner = "";
                    string grants = "";
                    string tags = "";
                    string lastaccess = "";
                    string defaultpage = "";
                    string website = "";
                    //Now start pulling der einen data.

                    GetACLRequest GACR = new GetACLRequest();
                    GACR.BucketName = name;
                    var ACL = BS3Client.GetACL(GACR);
                    var grantlist = ACL.AccessControlList;
                    owner = grantlist.Owner.DisplayName;
                    foreach (var agrant in grantlist.Grants)
                    {
                        if (grants.Length > 1) grants += "\n";
                        var gName = agrant.Grantee.DisplayName;
                        var gType = agrant.Grantee.Type.Value;
                        var aMail = agrant.Grantee.EmailAddress;

                        if (gType.Equals("Group"))
                        {
                            grants +=  gType + " - " + agrant.Grantee.URI + " - " + agrant.Permission + " - " + aMail  ;
                        }
                        else
                        {
                            grants += gName + " - "+ agrant.Permission + " - " + aMail;
                        }
                    }

                    GetObjectMetadataRequest request = new GetObjectMetadataRequest();
                    request.BucketName = name;
                    GetObjectMetadataResponse MDresponse = BS3Client.GetObjectMetadata(request);
                    lastaccess = MDresponse.LastModified.ToString();
                    //defaultpage = MDresponse.WebsiteRedirectLocation;

                    GetBucketWebsiteRequest GBWReq = new GetBucketWebsiteRequest();
                    GBWReq.BucketName = name;
                    GetBucketWebsiteResponse GBWRes = BS3Client.GetBucketWebsite(GBWReq);

                    defaultpage = GBWRes.WebsiteConfiguration.IndexDocumentSuffix;

                    if (defaultpage != null)
                    {
                        website = @"http://" + name + @".s3-website-" + region + @".amazonaws.com/" + defaultpage;
                    }

                    //Amazon.S3.Model.req

                    abucketrow["AccountID"] = accountid;
                    abucketrow["Profile"] = aprofile;
                    abucketrow["Bucket"] = name;
                    abucketrow["Region"] = region;
                    abucketrow["CreationDate"] = createddate.ToString();
                    abucketrow["LastAccess"] = lastaccess;
                    abucketrow["Owner"] = owner;
                    abucketrow["Grants"] = grants;

                    abucketrow["WebsiteHosting"] = website;
                    abucketrow["Logging"] = "X";
                    abucketrow["Events"] = "X";
                    abucketrow["Versioning"] = "X";
                    abucketrow["LifeCycle"] = "X";
                    abucketrow["Replication"] = "X";
                    abucketrow["Tags"] = "X";
                    abucketrow["RequesterPays"] = "X";
                    S3DetailsTable.Rows.Add(abucketrow.ItemArray);
                }

                }
                catch(Exception ex)
                {

                    System.Windows.Forms.MessageBox.Show("S3 Failed!\n"+ex);
                }

                #endregion

                #region GetEC2Region

                //////////////////////////////////////////////////////////

                //Foreach aregion
                foreach (var aregion in regions2process)
                {
                    //Skip GovCloud and Beijing. They require special handling and I dont need em.
                    if (aregion == Amazon.RegionEndpoint.USGovCloudWest1) continue;
                    if (aregion == Amazon.RegionEndpoint.CNNorth1) continue;
                    var region = aregion;

                    regioncounter++;

                    //Try to get scheduled events on my Profile/aregion
                    var ec2 = AWSClientFactory.CreateAmazonEC2Client(credential, region);
                    var request = new DescribeInstanceStatusRequest();
                    request.IncludeAllInstances = true;
                    Dispatcher.Invoke(doupdatePbDelegate,
                       System.Windows.Threading.DispatcherPriority.Background,
                        new object[] { System.Windows.Controls.ProgressBar.ValueProperty, regioncounter });
                    var instatresponse = ec2.DescribeInstanceStatus(request);

                    var indatarequest = new DescribeInstancesRequest();

                    foreach (var instat in instatresponse.InstanceStatuses)
                    {

                        indatarequest.InstanceIds.Add(instat.InstanceId);
                    }
                    DescribeInstancesResult DescResult = ec2.DescribeInstances(indatarequest);

                    int count = instatresponse.InstanceStatuses.Count();

                    foreach (var instat in instatresponse.InstanceStatuses)
                    {
                        //Collect the datases
                        string instanceid = instat.InstanceId;
                        string instancename = "";
                        ProcessingLabel.Content = "Scanning -> Profile:" + aprofile + "    Region: " + region + "   Instance: " + instanceid;
                        Dispatcher.Invoke(doupdatePbDelegate,
                            System.Windows.Threading.DispatcherPriority.Background,
                            new object[] { System.Windows.Controls.ProgressBar.ValueProperty, regioncounter });

                        var status = instat.Status.Status;
                        string AZ = instat.AvailabilityZone;
                        var istate = instat.InstanceState.Name;

                        string profile = aprofile;
                        string myregion = region.ToString();
                        int eventnumber = instat.Events.Count();

                        string eventlist = "";
                        var urtburgle = DescResult.Reservations;

                        string tags = ""; // Holds the list of tags to print out.

                        var loadtags = (from t in DescResult.Reservations
                                        where t.Instances[0].InstanceId.Equals(instanceid)
                                        select t.Instances[0].Tags).AsEnumerable();

                        Dictionary<string, string> taglist = new Dictionary<string, string>();
                        foreach (var rekey in loadtags)
                        {
                            foreach (var kvp in rekey)
                            {
                                taglist.Add(kvp.Key, kvp.Value);
                            }
                        }

                        foreach (var atag in taglist)//Set instancename, and add value to combobox.
                        {
                            if (atag.Key.Equals("Name"))
                            {
                                instancename = atag.Value;
                            }
                            if (!TagFilterCombo.Items.Contains(atag.Key))
                            {
                                TagFilterCombo.Items.Add(atag.Key);
                            }
                            if (tags.Length > 1)
                            {
                                tags += "\n" + atag.Key + ":" + atag.Value;
                            }
                            else
                            {
                                tags += atag.Key + ":" + atag.Value;
                            }
                        }

                        if (eventnumber > 0)
                        {
                            foreach (var anevent in instat.Events)
                            {
                                eventlist += anevent.Description + "\n";
                            }
                        }

                        var platform = (from t in urtburgle
                                        where t.Instances[0].InstanceId.Equals(instanceid)
                                        select t.Instances[0].Platform).FirstOrDefault();
                        if (String.IsNullOrEmpty(platform)) platform = "Linux";

                        var Priv_IP = (from t in urtburgle
                                       where t.Instances[0].InstanceId.Equals(instanceid)
                                       select t.Instances[0].PrivateIpAddress).FirstOrDefault();
                        if (String.IsNullOrEmpty(Priv_IP)) Priv_IP = "?";

                        var publicIP = (from t in urtburgle
                                        where t.Instances[0].InstanceId.Equals(instanceid)
                                        select t.Instances[0].PublicIpAddress).FirstOrDefault();
                        if (String.IsNullOrEmpty(publicIP)) publicIP = "";

                        var publicDNS = (from t in urtburgle
                                         where t.Instances[0].InstanceId.Equals(instanceid)
                                         select t.Instances[0].PublicDnsName).FirstOrDefault();
                        if (String.IsNullOrEmpty(publicDNS)) publicDNS = "";

                        //Virtualization type (HVM, Paravirtual)
                        var ivirtType = (from t in urtburgle
                                         where t.Instances[0].InstanceId.Equals(instanceid)
                                         select t.Instances[0].VirtualizationType).FirstOrDefault();
                        if (String.IsNullOrEmpty(ivirtType)) ivirtType = "?";

                        // InstanceType (m3/Large etc)
                        var instancetype = (from t in urtburgle
                                            where t.Instances[0].InstanceId.Equals(instanceid)
                                            select t.Instances[0].InstanceType).FirstOrDefault();
                        if (String.IsNullOrEmpty(instancetype)) instancetype = "?";

                        var SGs = (from t in urtburgle
                                   where t.Instances[0].InstanceId.Equals(instanceid)
                                   select t.Instances[0].SecurityGroups);

                        string sglist = "";

                        if (SGs.Count() > 0)
                        {
                            foreach (var ansg in SGs.FirstOrDefault())
                            {
                                if (sglist.Length > 2) { sglist += "\n"; }
                                sglist += ansg.GroupName;
                            }
                        }
                        else
                        {
                            sglist = "_NONE!_";
                        }
                        //Add to table
                        if (String.IsNullOrEmpty(sglist)) sglist = "NullOrEmpty";

                        if (String.IsNullOrEmpty(instancename)) instancename = "";
                        string rabbit = accountid + profile + myregion + instancename + instanceid + AZ + status + eventnumber + eventlist + tags + Priv_IP + publicIP + publicDNS + istate + ivirtType + instancetype + sglist;

                        if(instancename.Contains("p1-job"))
                        {
                            string yup = "y";
                        }

                        EC2DetailsTable.Rows.Add(accountid, profile, myregion, instancename, instanceid, AZ, platform, status, eventnumber, eventlist, tags, Priv_IP, publicIP, publicDNS, istate, ivirtType, instancetype, sglist);

                    }

                }
                #endregion
                ScanResults.Add("EC2", EC2DetailsTable);
                ScanResults.Add("Users", UserDetailsTable);
                ScanResults.Add("S3", S3DetailsTable);

                return ScanResults;
            }
            catch (Exception ex)
            {
                //If we failed to connect with creds.

                string error = new string(ex.ToString().TakeWhile(c => c != '\n').ToArray());
                System.Windows.MessageBox.Show(error, Request.Profile.ToString() + " credentials failed to work.\n");
                //Try to flag the menu item so it no longer selectable, and maybe make she red.
                System.Windows.Controls.MenuItem Proot = (System.Windows.Controls.MenuItem)this.MainMenu.Items[1];
                foreach (System.Windows.Controls.MenuItem amenuitem in Proot.Items)
                {
                    if (amenuitem.Header.ToString() == aprofile.ToString())
                    {
                        amenuitem.IsCheckable = false;
                        amenuitem.IsChecked = false;
                        amenuitem.Background = Brushes.Red;
                        amenuitem.ToolTip = Request.Profile.ToString() + " credentials failed to work.\n";
                    }
                }

                ScanResults.Add("EC2", GetEC2DetailsTable());
                ScanResults.Add("Users", GetUsersDetailsTable());
                ScanResults.Add("S3", GetS3DetailsTable());

                return ScanResults;

            }
        }
Ejemplo n.º 27
0
        public List<string> GetS3BucketNames()
        {
            var result = new List<string>();

            using (var s3Client = new Amazon.S3.AmazonS3Client(Credentials, Amazon.RegionEndpoint.USEast1))
            {
                var response = s3Client.ListBuckets();

                foreach (var item in response.Buckets)
                    result.Add(item.BucketName);
            }

            return result;
        }
Ejemplo n.º 28
0
        public static string GetServiceOutput()
        {
            StringBuilder sb = new StringBuilder(1024);
            using (StringWriter sr = new StringWriter(sb))
            {
                sr.WriteLine("===========================================");
                sr.WriteLine("Welcome to the AWS .NET SDK!");
                sr.WriteLine("===========================================");

                // Print the number of Amazon EC2 instances.
                IAmazonEC2 ec2 = new AmazonEC2Client();
                DescribeInstancesRequest ec2Request = new DescribeInstancesRequest();

                try
                {
                    DescribeInstancesResponse ec2Response = ec2.DescribeInstances(ec2Request);
                    int numInstances = 0;
                    numInstances = ec2Response.Reservations.Count;
                    sr.WriteLine(string.Format("You have {0} Amazon EC2 instance(s) running in the {1} region.",
                                               numInstances, ConfigurationManager.AppSettings["AWSRegion"]));
                }
                catch (AmazonEC2Exception ex)
                {
                    if (ex.ErrorCode != null && ex.ErrorCode.Equals("AuthFailure"))
                    {
                        sr.WriteLine("The account you are using is not signed up for Amazon EC2.");
                        sr.WriteLine("You can sign up for Amazon EC2 at http://aws.amazon.com/ec2");
                    }
                    else
                    {
                        sr.WriteLine("Caught Exception: " + ex.Message);
                        sr.WriteLine("Response Status Code: " + ex.StatusCode);
                        sr.WriteLine("Error Code: " + ex.ErrorCode);
                        sr.WriteLine("Error Type: " + ex.ErrorType);
                        sr.WriteLine("Request ID: " + ex.RequestId);
                    }
                }
                sr.WriteLine();

                // Print the number of Amazon SimpleDB domains.
                IAmazonSimpleDB sdb = new AmazonSimpleDBClient();
                ListDomainsRequest sdbRequest = new ListDomainsRequest();

                try
                {
                    ListDomainsResponse sdbResponse = sdb.ListDomains(sdbRequest);

                    int numDomains = 0;
                    numDomains = sdbResponse.DomainNames.Count;
                    sr.WriteLine(string.Format("You have {0} Amazon SimpleDB domain(s) in the {1} region.",
                                               numDomains, ConfigurationManager.AppSettings["AWSRegion"]));
                }
                catch (AmazonSimpleDBException ex)
                {
                    if (ex.ErrorCode != null && ex.ErrorCode.Equals("AuthFailure"))
                    {
                        sr.WriteLine("The account you are using is not signed up for Amazon SimpleDB.");
                        sr.WriteLine("You can sign up for Amazon SimpleDB at http://aws.amazon.com/simpledb");
                    }
                    else
                    {
                        sr.WriteLine("Caught Exception: " + ex.Message);
                        sr.WriteLine("Response Status Code: " + ex.StatusCode);
                        sr.WriteLine("Error Code: " + ex.ErrorCode);
                        sr.WriteLine("Error Type: " + ex.ErrorType);
                        sr.WriteLine("Request ID: " + ex.RequestId);
                    }
                }
                sr.WriteLine();

                // Print the number of Amazon S3 Buckets.
                IAmazonS3 s3Client = new AmazonS3Client();

                try
                {
                    ListBucketsResponse response = s3Client.ListBuckets();
                    int numBuckets = 0;
                    if (response.Buckets != null &&
                        response.Buckets.Count > 0)
                    {
                        numBuckets = response.Buckets.Count;
                    }
                    sr.WriteLine("You have " + numBuckets + " Amazon S3 bucket(s).");
                }
                catch (AmazonS3Exception ex)
                {
                    if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") ||
                        ex.ErrorCode.Equals("InvalidSecurity")))
                    {
                        sr.WriteLine("Please check the provided AWS Credentials.");
                        sr.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
                    }
                    else
                    {
                        sr.WriteLine("Caught Exception: " + ex.Message);
                        sr.WriteLine("Response Status Code: " + ex.StatusCode);
                        sr.WriteLine("Error Code: " + ex.ErrorCode);
                        sr.WriteLine("Request ID: " + ex.RequestId);
                    }
                }
                sr.WriteLine("Press any key to continue...");
            }
            return sb.ToString();
        }