Amazon S3 Bucket Management with C#: Part 7 – Creating a text file in a Bucket
Before getting started
Skill Level: Beginner
Assumptions:
- You already gone through Parts 1-6 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
- Rhyous.StringLibrary
Step 1 – Add NuGet Package
- Right-click on your project and choose Management NuGet Packages.
- Search for Rhyous.StringLibrary.
This is a simple library for string extensions methods and more. String code that is not in .Net by default, yet the methods have proven over time to be commonly used. - Install the Rhyous.StringLibrary NuGet package.
Step 2 – Add a CreateTextFile method to BucketManager.cs
- Edit file called BucketManager.cs.
- Enter this new method:
public static async Task CreateTextFile(AmazonS3Client client, string bucketName, string filename, string text) { var dirRequest = new PutObjectRequest { BucketName = bucketName, Key = filename, InputStream = text.ToStream() }; await client.PutObjectAsync(dirRequest); Console.WriteLine($"Created text file in S3 bucket: {bucketName}/{filename}"); }
Notice: The code is almost identical to that of creating a directory, with two exceptions. We aren’t ending with a /. And instead of assigning zero bytes to InputStream, we assigned text.ToStream(). Rhyous.StringLibrary provides us the ToStream() extension method.
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", "CreateTextFile", }, ...
Step 3 – Add FileName and Text Arguments
If we are going to create a text file, we need to know the file name and the text to insert.
- Edit the ArgsHandler.cs file to define an Action argument.
... new Argument { Name = "FileName", ShortName = "N", Description = "The name of text a file to create.", Example = "{name}=MyTextfile.txt", Action = (value) => { Console.WriteLine(value); } }, new Argument { Name = "Text", ShortName = "T", Description = "The text to put in a text file.", Example = "{name}=\"This is some text!\"", Action = (value) => { Console.WriteLine(value); } } ...
Step 4 – 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.
However, take note that we now have more exceptions than we had commonalities. This suggests that it is about time to refactor this code. For now, we will leave it.
// 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") }); CustomParameters.Add("CreateTextFile", new object[] { s3client, bucketName, Args.Value("Filename"), Args.Value("Text") });
You can now add a text file to an Amazon S3 bucket using C#.
Go to: Deleting a file in a Bucket
Return to: Managing Amazon AWS with C#