Work with Amazon S3 object lock features using an AWS SDK
The following code examples show how to work with S3 object lock features.
- AWS SDK for .NET
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Run an interactive scenario demonstrating Amazon S3 object lock features.
using Amazon.S3; using Amazon.S3.Model; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Microsoft.Extensions.Logging.Debug; namespace S3ObjectLockScenario; public static class S3ObjectLockWorkflow { /* Before running this .NET code example, set up your development environment, including your credentials. This .NET example performs the following tasks: 1. Create test Amazon Simple Storage Service (S3) buckets with different lock policies. 2. Upload sample objects to each bucket. 3. Set some Legal Hold and Retention Periods on objects and buckets. 4. Investigate lock policies by viewing settings or attempting to delete or overwrite objects. 5. Clean up objects and buckets. */ public static S3ActionsWrapper _s3ActionsWrapper = null!; public static IConfiguration _configuration = null!; private static string _resourcePrefix = null!; private static string noLockBucketName = null!; private static string lockEnabledBucketName = null!; private static string retentionAfterCreationBucketName = null!; private static List<string> bucketNames = new List<string>(); private static List<string> fileNames = new List<string>(); public static async Task Main(string[] args) { // Set up dependency injection for the Amazon service. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonS3>() .AddTransient<S3ActionsWrapper>() ) .Build(); _configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load settings from .json file. .AddJsonFile("settings.local.json", true) // Optionally, load local settings. .Build(); ConfigurationSetup(); ServicesSetup(host); try { Console.WriteLine(new string('-', 80)); Console.WriteLine("Welcome to the Amazon Simple Storage Service (S3) Object Locking Feature Scenario."); Console.WriteLine(new string('-', 80)); await Setup(true); await DemoActionChoices(); Console.WriteLine(new string('-', 80)); Console.WriteLine("Cleaning up resources."); Console.WriteLine(new string('-', 80)); await Cleanup(true); Console.WriteLine(new string('-', 80)); Console.WriteLine("Amazon S3 Object Locking Scenario is complete."); Console.WriteLine(new string('-', 80)); } catch (Exception ex) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"There was a problem: {ex.Message}"); await Cleanup(true); Console.WriteLine(new string('-', 80)); } } /// <summary> /// Populate the services for use within the console application. /// </summary> /// <param name="host">The services host.</param> private static void ServicesSetup(IHost host) { _s3ActionsWrapper = host.Services.GetRequiredService<S3ActionsWrapper>(); } /// <summary> /// Any setup operations needed. /// </summary> public static void ConfigurationSetup() { _resourcePrefix = _configuration["resourcePrefix"] ?? "dotnet-example"; noLockBucketName = _resourcePrefix + "-no-lock"; lockEnabledBucketName = _resourcePrefix + "-lock-enabled"; retentionAfterCreationBucketName = _resourcePrefix + "-retention-after-creation"; bucketNames.Add(noLockBucketName); bucketNames.Add(lockEnabledBucketName); bucketNames.Add(retentionAfterCreationBucketName); } // <summary> /// Deploy necessary resources for the scenario. /// </summary> /// <param name="interactive">True to run as interactive.</param> /// <returns>True if successful.</returns> public static async Task<bool> Setup(bool interactive) { Console.WriteLine( "\nFor this scenario, we will use the AWS SDK for .NET to create several S3\n" + "buckets and files to demonstrate working with S3 locking features.\n"); Console.WriteLine(new string('-', 80)); Console.WriteLine("Press Enter when you are ready to start."); if (interactive) Console.ReadLine(); Console.WriteLine("\nS3 buckets can be created either with or without object lock enabled."); await _s3ActionsWrapper.CreateBucketWithObjectLock(noLockBucketName, false); await _s3ActionsWrapper.CreateBucketWithObjectLock(lockEnabledBucketName, true); await _s3ActionsWrapper.CreateBucketWithObjectLock(retentionAfterCreationBucketName, false); Console.WriteLine("Press Enter to continue."); if (interactive) Console.ReadLine(); Console.WriteLine("\nA bucket can be configured to use object locking with a default retention period."); await _s3ActionsWrapper.ModifyBucketDefaultRetention(retentionAfterCreationBucketName, true, ObjectLockRetentionMode.Governance, DateTime.UtcNow.AddDays(1)); Console.WriteLine("Press Enter to continue."); if (interactive) Console.ReadLine(); Console.WriteLine("\nObject lock policies can also be added to existing buckets."); await _s3ActionsWrapper.EnableObjectLockOnBucket(lockEnabledBucketName); Console.WriteLine("Press Enter to continue."); if (interactive) Console.ReadLine(); // Upload some files to the buckets. Console.WriteLine("\nNow let's add some test files:"); var fileName = _configuration["exampleFileName"] ?? "exampleFile.txt"; int fileCount = 2; // Create the file if it does not already exist. if (!File.Exists(fileName)) { await using StreamWriter sw = File.CreateText(fileName); await sw.WriteLineAsync( "This is a sample file for uploading to a bucket."); } foreach (var bucketName in bucketNames) { for (int i = 0; i < fileCount; i++) { var numberedFileName = Path.GetFileNameWithoutExtension(fileName) + i + Path.GetExtension(fileName); fileNames.Add(numberedFileName); await _s3ActionsWrapper.UploadFileAsync(bucketName, numberedFileName, fileName); } } Console.WriteLine("Press Enter to continue."); if (interactive) Console.ReadLine(); if (!interactive) return true; Console.WriteLine("\nNow we can set some object lock policies on individual files:"); foreach (var bucketName in bucketNames) { for (int i = 0; i < fileNames.Count; i++) { // No modifications to the objects in the first bucket. if (bucketName != bucketNames[0]) { var exampleFileName = fileNames[i]; switch (i) { case 0: { var question = $"\nWould you like to add a legal hold to {exampleFileName} in {bucketName}? (y/n)"; if (GetYesNoResponse(question)) { // Set a legal hold. await _s3ActionsWrapper.ModifyObjectLegalHold(bucketName, exampleFileName, ObjectLockLegalHoldStatus.On); } break; } case 1: { var question = $"\nWould you like to add a 1 day Governance retention period to {exampleFileName} in {bucketName}? (y/n)" + "\nReminder: Only a user with the s3:BypassGovernanceRetention permission will be able to delete this file or its bucket until the retention period has expired."; if (GetYesNoResponse(question)) { // Set a Governance mode retention period for 1 day. await _s3ActionsWrapper.ModifyObjectRetentionPeriod( bucketName, exampleFileName, ObjectLockRetentionMode.Governance, DateTime.UtcNow.AddDays(1)); } break; } } } } } Console.WriteLine(new string('-', 80)); return true; } // <summary> /// List all of the current buckets and objects. /// </summary> /// <param name="interactive">True to run as interactive.</param> /// <returns>The list of buckets and objects.</returns> public static async Task<List<S3ObjectVersion>> ListBucketsAndObjects(bool interactive) { var allObjects = new List<S3ObjectVersion>(); foreach (var bucketName in bucketNames) { var objectsInBucket = await _s3ActionsWrapper.ListBucketObjectsAndVersions(bucketName); foreach (var objectKey in objectsInBucket.Versions) { allObjects.Add(objectKey); } } if (interactive) { Console.WriteLine("\nCurrent buckets and objects:\n"); int i = 0; foreach (var bucketObject in allObjects) { i++; Console.WriteLine( $"{i}: {bucketObject.Key} \n\tBucket: {bucketObject.BucketName}\n\tVersion: {bucketObject.VersionId}"); } } return allObjects; } /// <summary> /// Present the user with the demo action choices. /// </summary> /// <returns>Async task.</returns> public static async Task<bool> DemoActionChoices() { var choices = new string[]{ "List all files in buckets.", "Attempt to delete a file.", "Attempt to delete a file with retention period bypass.", "Attempt to overwrite a file.", "View the object and bucket retention settings for a file.", "View the legal hold settings for a file.", "Finish the scenario."}; var choice = 0; // Keep asking the user until they choose to move on. while (choice != 6) { Console.WriteLine(new string('-', 80)); choice = GetChoiceResponse( "\nExplore the S3 locking features by selecting one of the following choices:" , choices); Console.WriteLine(new string('-', 80)); switch (choice) { case 0: { await ListBucketsAndObjects(true); break; } case 1: { Console.WriteLine("\nEnter the number of the object to delete:"); var allFiles = await ListBucketsAndObjects(true); var fileChoice = GetChoiceResponse(null, allFiles.Select(f => f.Key).ToArray()); await _s3ActionsWrapper.DeleteObjectFromBucket(allFiles[fileChoice].BucketName, allFiles[fileChoice].Key, false, allFiles[fileChoice].VersionId); break; } case 2: { Console.WriteLine("\nEnter the number of the object to delete:"); var allFiles = await ListBucketsAndObjects(true); var fileChoice = GetChoiceResponse(null, allFiles.Select(f => f.Key).ToArray()); await _s3ActionsWrapper.DeleteObjectFromBucket(allFiles[fileChoice].BucketName, allFiles[fileChoice].Key, true, allFiles[fileChoice].VersionId); break; } case 3: { var allFiles = await ListBucketsAndObjects(true); Console.WriteLine("\nEnter the number of the object to overwrite:"); var fileChoice = GetChoiceResponse(null, allFiles.Select(f => f.Key).ToArray()); // Create the file if it does not already exist. if (!File.Exists(allFiles[fileChoice].Key)) { await using StreamWriter sw = File.CreateText(allFiles[fileChoice].Key); await sw.WriteLineAsync( "This is a sample file for uploading to a bucket."); } await _s3ActionsWrapper.UploadFileAsync(allFiles[fileChoice].BucketName, allFiles[fileChoice].Key, allFiles[fileChoice].Key); break; } case 4: { var allFiles = await ListBucketsAndObjects(true); Console.WriteLine("\nEnter the number of the object and bucket to view:"); var fileChoice = GetChoiceResponse(null, allFiles.Select(f => f.Key).ToArray()); await _s3ActionsWrapper.GetObjectRetention(allFiles[fileChoice].BucketName, allFiles[fileChoice].Key); await _s3ActionsWrapper.GetBucketObjectLockConfiguration(allFiles[fileChoice].BucketName); break; } case 5: { var allFiles = await ListBucketsAndObjects(true); Console.WriteLine("\nEnter the number of the object to view:"); var fileChoice = GetChoiceResponse(null, allFiles.Select(f => f.Key).ToArray()); await _s3ActionsWrapper.GetObjectLegalHold(allFiles[fileChoice].BucketName, allFiles[fileChoice].Key); break; } } } return true; } // <summary> /// Clean up the resources from the scenario. /// </summary> /// <param name="interactive">True to run as interactive.</param> /// <returns>True if successful.</returns> public static async Task<bool> Cleanup(bool interactive) { Console.WriteLine(new string('-', 80)); if (!interactive || GetYesNoResponse("Do you want to clean up all files and buckets? (y/n) ")) { // Remove all locks and delete all buckets and objects. var allFiles = await ListBucketsAndObjects(false); foreach (var fileInfo in allFiles) { // Check for a legal hold. var legalHold = await _s3ActionsWrapper.GetObjectLegalHold(fileInfo.BucketName, fileInfo.Key); if (legalHold?.Status?.Value == ObjectLockLegalHoldStatus.On) { await _s3ActionsWrapper.ModifyObjectLegalHold(fileInfo.BucketName, fileInfo.Key, ObjectLockLegalHoldStatus.Off); } // Check for a retention period. var retention = await _s3ActionsWrapper.GetObjectRetention(fileInfo.BucketName, fileInfo.Key); var hasRetentionPeriod = retention?.Mode == ObjectLockRetentionMode.Governance && retention.RetainUntilDate > DateTime.UtcNow.Date; await _s3ActionsWrapper.DeleteObjectFromBucket(fileInfo.BucketName, fileInfo.Key, hasRetentionPeriod, fileInfo.VersionId); } foreach (var bucketName in bucketNames) { await _s3ActionsWrapper.DeleteBucketByName(bucketName); } } else { Console.WriteLine( "Ok, we'll leave the resources intact.\n" + "Don't forget to delete them when you're done with them or you might incur unexpected charges." ); } Console.WriteLine(new string('-', 80)); return true; } /// <summary> /// Helper method to get a yes or no response from the user. /// </summary> /// <param name="question">The question string to print on the console.</param> /// <returns>True if the user responds with a yes.</returns> private static bool GetYesNoResponse(string question) { Console.WriteLine(question); var ynResponse = Console.ReadLine(); var response = ynResponse != null && ynResponse.Equals("y", StringComparison.InvariantCultureIgnoreCase); return response; } /// <summary> /// Helper method to get a choice response from the user. /// </summary> /// <param name="question">The question string to print on the console.</param> /// <param name="choices">The choices to print on the console.</param> /// <returns>The index of the selected choice</returns> private static int GetChoiceResponse(string? question, string[] choices) { if (question != null) { Console.WriteLine(question); for (int i = 0; i < choices.Length; i++) { Console.WriteLine($"\t{i + 1}. {choices[i]}"); } } var choiceNumber = 0; while (choiceNumber < 1 || choiceNumber > choices.Length) { var choice = Console.ReadLine(); Int32.TryParse(choice, out choiceNumber); } return choiceNumber - 1; } }
A wrapper class for S3 functions.
using System.Net; using Amazon.S3; using Amazon.S3.Model; using Microsoft.Extensions.Configuration; namespace S3ObjectLockScenario; /// <summary> /// Encapsulate the Amazon S3 operations. /// </summary> public class S3ActionsWrapper { private readonly IAmazonS3 _amazonS3; /// <summary> /// Constructor for the S3ActionsWrapper. /// </summary> /// <param name="amazonS3">The injected S3 client.</param> public S3ActionsWrapper(IAmazonS3 amazonS3, IConfiguration configuration) { _amazonS3 = amazonS3; } /// <summary> /// Create a new Amazon S3 bucket with object lock actions. /// </summary> /// <param name="bucketName">The name of the bucket to create.</param> /// <param name="enableObjectLock">True to enable object lock on the bucket.</param> /// <returns>True if successful.</returns> public async Task<bool> CreateBucketWithObjectLock(string bucketName, bool enableObjectLock) { Console.WriteLine($"\tCreating bucket {bucketName} with object lock {enableObjectLock}."); try { var request = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true, ObjectLockEnabledForBucket = enableObjectLock, }; var response = await _amazonS3.PutBucketAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error creating bucket: '{ex.Message}'"); return false; } } /// <summary> /// Enable object lock on an existing bucket. /// </summary> /// <param name="bucketName">The name of the bucket to modify.</param> /// <returns>True if successful.</returns> public async Task<bool> EnableObjectLockOnBucket(string bucketName) { try { // First, enable Versioning on the bucket. await _amazonS3.PutBucketVersioningAsync(new PutBucketVersioningRequest() { BucketName = bucketName, VersioningConfig = new S3BucketVersioningConfig() { EnableMfaDelete = false, Status = VersionStatus.Enabled } }); var request = new PutObjectLockConfigurationRequest() { BucketName = bucketName, ObjectLockConfiguration = new ObjectLockConfiguration() { ObjectLockEnabled = new ObjectLockEnabled("Enabled"), }, }; var response = await _amazonS3.PutObjectLockConfigurationAsync(request); Console.WriteLine($"\tAdded an object lock policy to bucket {bucketName}."); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error modifying object lock: '{ex.Message}'"); return false; } } /// <summary> /// Set or modify a retention period on an object in an S3 bucket. /// </summary> /// <param name="bucketName">The bucket of the object.</param> /// <param name="objectKey">The key of the object.</param> /// <param name="retention">The retention mode.</param> /// <param name="retainUntilDate">The date retention expires.</param> /// <returns>True if successful.</returns> public async Task<bool> ModifyObjectRetentionPeriod(string bucketName, string objectKey, ObjectLockRetentionMode retention, DateTime retainUntilDate) { try { var request = new PutObjectRetentionRequest() { BucketName = bucketName, Key = objectKey, Retention = new ObjectLockRetention() { Mode = retention, RetainUntilDate = retainUntilDate } }; var response = await _amazonS3.PutObjectRetentionAsync(request); Console.WriteLine($"\tSet retention for {objectKey} in {bucketName} until {retainUntilDate:d}."); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"\tError modifying retention period: '{ex.Message}'"); return false; } } /// <summary> /// Set or modify a retention period on an S3 bucket. /// </summary> /// <param name="bucketName">The bucket to modify.</param> /// <param name="retention">The retention mode.</param> /// <param name="retainUntilDate">The date for retention until.</param> /// <returns>True if successful.</returns> public async Task<bool> ModifyBucketDefaultRetention(string bucketName, bool enableObjectLock, ObjectLockRetentionMode retention, DateTime retainUntilDate) { var enabledString = enableObjectLock ? "Enabled" : "Disabled"; var timeDifference = retainUntilDate.Subtract(DateTime.Now); try { // First, enable Versioning on the bucket. await _amazonS3.PutBucketVersioningAsync(new PutBucketVersioningRequest() { BucketName = bucketName, VersioningConfig = new S3BucketVersioningConfig() { EnableMfaDelete = false, Status = VersionStatus.Enabled } }); var request = new PutObjectLockConfigurationRequest() { BucketName = bucketName, ObjectLockConfiguration = new ObjectLockConfiguration() { ObjectLockEnabled = new ObjectLockEnabled(enabledString), Rule = new ObjectLockRule() { DefaultRetention = new DefaultRetention() { Mode = retention, Days = timeDifference.Days // Can be specified in days or years but not both. } } } }; var response = await _amazonS3.PutObjectLockConfigurationAsync(request); Console.WriteLine($"\tAdded a default retention to bucket {bucketName}."); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"\tError modifying object lock: '{ex.Message}'"); return false; } } /// <summary> /// Get the retention period for an S3 object. /// </summary> /// <param name="bucketName">The bucket of the object.</param> /// <param name="objectKey">The object key.</param> /// <returns>The object retention details.</returns> public async Task<ObjectLockRetention> GetObjectRetention(string bucketName, string objectKey) { try { var request = new GetObjectRetentionRequest() { BucketName = bucketName, Key = objectKey }; var response = await _amazonS3.GetObjectRetentionAsync(request); Console.WriteLine($"\tObject retention for {objectKey} in {bucketName}: " + $"\n\t{response.Retention.Mode} until {response.Retention.RetainUntilDate:d}."); return response.Retention; } catch (AmazonS3Exception ex) { Console.WriteLine($"\tUnable to fetch object lock retention: '{ex.Message}'"); return new ObjectLockRetention(); } } /// <summary> /// Set or modify a legal hold on an object in an S3 bucket. /// </summary> /// <param name="bucketName">The bucket of the object.</param> /// <param name="objectKey">The key of the object.</param> /// <param name="holdStatus">The On or Off status for the legal hold.</param> /// <returns>True if successful.</returns> public async Task<bool> ModifyObjectLegalHold(string bucketName, string objectKey, ObjectLockLegalHoldStatus holdStatus) { try { var request = new PutObjectLegalHoldRequest() { BucketName = bucketName, Key = objectKey, LegalHold = new ObjectLockLegalHold() { Status = holdStatus } }; var response = await _amazonS3.PutObjectLegalHoldAsync(request); Console.WriteLine($"\tModified legal hold for {objectKey} in {bucketName}."); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"\tError modifying legal hold: '{ex.Message}'"); return false; } } /// <summary> /// Get the legal hold details for an S3 object. /// </summary> /// <param name="bucketName">The bucket of the object.</param> /// <param name="objectKey">The object key.</param> /// <returns>The object legal hold details.</returns> public async Task<ObjectLockLegalHold> GetObjectLegalHold(string bucketName, string objectKey) { try { var request = new GetObjectLegalHoldRequest() { BucketName = bucketName, Key = objectKey }; var response = await _amazonS3.GetObjectLegalHoldAsync(request); Console.WriteLine($"\tObject legal hold for {objectKey} in {bucketName}: " + $"\n\tStatus: {response.LegalHold.Status}"); return response.LegalHold; } catch (AmazonS3Exception ex) { Console.WriteLine($"\tUnable to fetch legal hold: '{ex.Message}'"); return new ObjectLockLegalHold(); } } /// <summary> /// Get the object lock configuration details for an S3 bucket. /// </summary> /// <param name="bucketName">The bucket to get details.</param> /// <returns>The bucket's object lock configuration details.</returns> public async Task<ObjectLockConfiguration> GetBucketObjectLockConfiguration(string bucketName) { try { var request = new GetObjectLockConfigurationRequest() { BucketName = bucketName }; var response = await _amazonS3.GetObjectLockConfigurationAsync(request); Console.WriteLine($"\tBucket object lock config for {bucketName} in {bucketName}: " + $"\n\tEnabled: {response.ObjectLockConfiguration.ObjectLockEnabled}" + $"\n\tRule: {response.ObjectLockConfiguration.Rule?.DefaultRetention}"); return response.ObjectLockConfiguration; } catch (AmazonS3Exception ex) { Console.WriteLine($"\tUnable to fetch object lock config: '{ex.Message}'"); return new ObjectLockConfiguration(); } } /// <summary> /// Upload a file from the local computer to an Amazon S3 bucket. /// </summary> /// <param name="bucketName">The Amazon S3 bucket to use.</param> /// <param name="objectName">The object to upload.</param> /// <param name="filePath">The path, including file name, of the object to upload.</param> /// <returns>True if success.<returns> public async Task<bool> UploadFileAsync(string bucketName, string objectName, string filePath) { var request = new PutObjectRequest { BucketName = bucketName, Key = objectName, FilePath = filePath, ChecksumAlgorithm = ChecksumAlgorithm.SHA256 }; var response = await _amazonS3.PutObjectAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"\tSuccessfully uploaded {objectName} to {bucketName}."); return true; } else { Console.WriteLine($"\tCould not upload {objectName} to {bucketName}."); return false; } } /// <summary> /// List bucket objects and versions. /// </summary> /// <param name="bucketName">The Amazon S3 bucket to use.</param> /// <returns>The list of objects and versions.</returns> public async Task<ListVersionsResponse> ListBucketObjectsAndVersions(string bucketName) { var request = new ListVersionsRequest() { BucketName = bucketName }; var response = await _amazonS3.ListVersionsAsync(request); return response; } /// <summary> /// Delete an object from a specific bucket. /// </summary> /// <param name="bucketName">The Amazon S3 bucket to use.</param> /// <param name="objectKey">The key of the object to delete.</param> /// <param name="hasRetention">True if the object has retention settings.</param> /// <param name="versionId">Optional versionId.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteObjectFromBucket(string bucketName, string objectKey, bool hasRetention, string? versionId = null) { try { var request = new DeleteObjectRequest() { BucketName = bucketName, Key = objectKey, VersionId = versionId, }; if (hasRetention) { // Set the BypassGovernanceRetention header // if the file has retention settings. request.BypassGovernanceRetention = true; } await _amazonS3.DeleteObjectAsync(request); Console.WriteLine( $"Deleted {objectKey} in {bucketName}."); return true; } catch (AmazonS3Exception ex) { Console.WriteLine($"\tUnable to delete object {objectKey} in bucket {bucketName}: " + ex.Message); return false; } } /// <summary> /// Delete a specific bucket. /// </summary> /// <param name="bucketName">The Amazon S3 bucket to use.</param> /// <param name="objectKey">The key of the object to delete.</param> /// <param name="versionId">Optional versionId.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteBucketByName(string bucketName) { try { var request = new DeleteBucketRequest() { BucketName = bucketName, }; var response = await _amazonS3.DeleteBucketAsync(request); Console.WriteLine($"\tDelete for {bucketName} complete."); return response.HttpStatusCode == HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"\tUnable to delete bucket {bucketName}: " + ex.Message); return false; } } }
-
For API details, see the following topics in AWS SDK for .NET API Reference.
-
For a complete list of AWS SDK developer guides and code examples, see Developing with Amazon S3 using the AWS SDKs. This topic also includes information about getting started and details about previous SDK versions.