Amazon S3 Bucket Management with C#: Part 6 – Deleting a Directory in a Bucket
Before getting started
Skill Level: Beginner
Assumptions:
- You already gone through Parts 1-5 of Managing Amazon AWS with C#.
Additional information: I sometimes cover small sub-topics in a post. Along with AWS, you will also be exposed to:
- Rhyous.SimpleArgs
Step 1 – Add a DeleteBucketDirectory method to BucketManager.cs
- Edit file called BucketManager.cs.
- Enter this new method:
public static async Task DeleteBucketDirectory(AmazonS3Client client, string bucketName, string directory) { var dirRequest = new DeleteObjectRequest { BucketName = bucketName, Key = directory + "/" }; await client.DeleteObjectAsync(dirRequest); Console.WriteLine($"Created S3 bucket folder: {bucketName}/{directory}/"); }
Note: Amazon S3 uses objects with a key ending in a / as a directory, so we have to call DeleteObjectAsync.
Step 2 – Update the Action Argument
We now need to make this method a valid action for the Action Argument.
- Edit the ArgsHandler.cs file to define an Action argument.
... AllowedValues = new ObservableCollection<string> { "CreateBucket", "DeleteBucket", "ListFiles", "UploadFile", "CreateBucketDirectory", "DeleteBucketDirectory" }, ...
Step 3 – Fix the parameter mismatch problem
In Part 4, we created a method to pass different parameters to different methods. Let’s use that to pass in the correct parameters.
// Use the Custom or Common pattern CustomParameters.Add("UploadFile", new object[] { transferUtility, bucketName, Args.Value("File") }); CustomParameters.Add("CreateBucketDirectory", new object[] { s3client, bucketName, Args.Value("Directory") }); CustomParameters.Add("DeleteBucketDirectory", new object[] { s3client, bucketName, Args.Value("Directory") });
You can now Delete a directory on S3, using C#.
Go to:
Return to: Managing Amazon AWS with C#