AWS S3 Command Line Bucket File Upload in C#

AWS S3 Command Line Bucket File Upload in C#

I generate full-size backups of my database every night, but they just collect dust on my production server taking up space.
Not only that, but if I lost the server, the backups would be gone to, so I wanted to investigate some options.

I have recently moved my servers and database to Amazon Web Services and really enjoying the value and flexibility of the platform. So I settled on Amazon Simple Storage Service (S3) to keep everything together. After downloading the SDK from their website, I decided to make a console application I could include in the backup script that is run. I couldn’t believe how simple it was to get going.

You also need to let the application know of your AWSAccessKey and AWSSecretKey, you can do that in two ways. Firstly pass them into the constructor of the AWSClientFactory or you can use the App.config.

That is essentially it, you pass a FileInfo object of the file, the name of the bucket (I created mine in AWS Management Console) and an optional prefix. The reason I added the prefix is because I wanted to store the backups in a folder, not in the root of the buckets, so for example I use db-backups/. Some other helpful attributes to include into the request object are:

Pretty standard code, you run it with a file as the first parameter and a bucket as the second. You can also pass in an optional prefix parameter.
I also wanted the ability to include all files in a folder, such as s3.exe c:\backups\* bucket-name so I added this before the var file line.

And that’s all folks. I know there are clients you can download to do this, but I think I enjoy understanding how it works by doing it myself. Also this is very easy to extend into a more versatile S3/AWS command line interface so I may post future updates later.