AWS S3 Create folder, upload files and create versions using .NET SDK

In this article we will learn how to create folder and upload file in S3 bucket. After this we will also learn how to create versions of file on S3.

Before proceeding with this example you should read my previous post Getting started with S3 (prerequisites).

For creating a folder on S3 we just need to simple use PutObjectRequest class and specify parameter then call PutObject method. See below example.

This will create the folder with the name you have provided in Key attribute of request object in specified bucket. Remember that all the credentials are already specified in config file so we don’t need to pass them here again. If you have not specified them in config file you can simple pass them in AmazonS3Client constructor. Same method is used to upload file in the bucket, only difference is Key attribute will contain full path of the file in bucket and we also have to set InputStream of request object to the file stream we want to upload. Below will be the final code for uploading file.

AmazonS3Client client = new AmazonS3Client();

PutObjectRequest request = new PutObjectRequest();

request.Key = “reports/q2review/report.xlsx”;
request.InputStream = FileStream;
request.BucketName = "SumitsBucket";
request.CannedACL = S3CannedACL.PublicRead;
request.StorageClass = S3StorageClass.Standard;

client.PutObject(request);
client.Dispose()

If you have not created folder with name reports and q2review in reports, no need to worry this request will automatically created folder and upload file in the path specified.

After creating folder upload file you might want to create various version of the same file. For this we have to enable versioning on our bucket. To enable versioning, Select you bucket in S3 Management Console and click open properties. Now click on Versioning tab and click .

After that you will be able to get unique version number for each upload file in response to PutObject method.

string version = client.PutObject(request).VersionId;

Yes, it is that much simple.

Note: In above example we have upload file with public read only. You can change the S3CannedACL to change the file permissions. One of the other possible option could be S3CannedACL.Private. This type of uploaded file will only be accessible if you sen request using your AWS credentials.

Leave a Reply