DeleteBucket() 공개 메소드

Deletes the bucket. All objects (including all object versions and Delete Markers) in the bucket must be deleted before the bucket itself can be deleted.
public DeleteBucket ( DeleteBucketRequest request ) : DeleteBucketResponse
request Amazon.S3.Model.DeleteBucketRequest Container for the necessary parameters to execute the DeleteBucket service method.
리턴 DeleteBucketResponse
예제 #1
0
 public static void DeleteBucket(string bucketName)
 {
     using (var client = new AmazonS3Client(Settings.AccessKey, Settings.Secret))
     {
         client.DeleteBucket(bucketName);
     }
 }
예제 #2
0
        public virtual void DeleteBucket(AmazonS3Client s3Client, string bucketName)
        {
            // First, try to delete the bucket.
            var deleteBucketRequest = new DeleteBucketRequest
            {
                BucketName = bucketName
            };

            try
            {
                s3Client.DeleteBucket(deleteBucketRequest);
                // If we get here, no error was generated so we'll assume the bucket was deleted and return.
                return;
            }
            catch (AmazonS3Exception ex)
            {
                if (!ex.ErrorCode.Equals("BucketNotEmpty"))
                {
                    // We got an unanticipated error. Just rethrow.
                    throw;
                }
            }

            // If we got here, then our bucket isn't empty so we need to delete the items in it first.

            DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest {BucketName = bucketName};

            foreach (S3Object obj in s3Client.ListObjects(new ListObjectsRequest {BucketName = bucketName}).S3Objects)
            {
                // Add keys for the objects to the delete request
                deleteObjectsRequest.AddKey(obj.Key, null);
            }

            // Submit the request
            s3Client.DeleteObjects(deleteObjectsRequest);

            // The bucket is empty now, so delete the bucket.
            s3Client.DeleteBucket(deleteBucketRequest);
        }
예제 #3
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);
                }
            }
        }
예제 #4
0
        public virtual void RemoveLabBuckets(AmazonS3Client s3Client, List<string> bucketNames)
        {
            foreach (var bucketName in bucketNames)
            {
                try
                {
                    ListObjectsResponse listObjectsResponse =
                        s3Client.ListObjects(new ListObjectsRequest {BucketName = bucketName});
                    foreach (var s3Object in listObjectsResponse.S3Objects)
                    {
                        var deleteObjectRequest = new DeleteObjectRequest
                        {
                            BucketName = bucketName,
                            Key = s3Object.Key
                        };
                        s3Client.DeleteObject(deleteObjectRequest);
                    }

                    s3Client.DeleteBucket(new DeleteBucketRequest {BucketName = bucketName});
                }
                catch (AmazonS3Exception s3E)
                {
                    if (!s3E.ErrorCode.Equals("NoSuchBucket"))
                    {
                        // This error wasn't expected, so rethrow.
                        throw;
                    }
                }
            }
        }
예제 #5
0
 /// <summary>
 /// delete bucket from s3
 /// </summary>
 /// <param name="PathName"></param>
 /// <returns></returns>
 public bool DeleteDirectory(string PathName)
 {
     try
     {
         //amazon client
         using (var client = new AmazonS3Client())
         {
             //delete bucket request
             using (var response = client.DeleteBucket(new DeleteBucketRequest()
                 .WithBucketName(PathName)))
             {
                 return true;
             }
         }
     }
     catch (Exception ex)
     {
         Trace.WriteLine(ex.ToString());
     }
     return false;
 }
예제 #6
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
            }
        }