Interacting with AWS S3 in Unity:

Jon Jenkins
4 min readNov 10, 2021

Here I want to list the basic steps of setting up an S3 interaction from Unity. I will go over how to set it up and then how to perform a couple of basic operations: Listing buckets and uploading objects to a bucket.

For detailed information refer to the AWS docs here:

Amazon Simple Storage Service (S3) — AWS Mobile SDK for Unity

I’m going to assume everyone is familiar with what AWS S3 is and that everyone has some buckets somewhere on S3. Also I’m assuming everyone has downloaded the AWS SDK for Unity. If not, see my previous article about getting started with AWS for Unity.

Make sure you are using the following namespaces:

First, how do we initialize our interaction with S3?

We start by setting our region:

The string above will be specific to whatever region your S3 bucket is in.

Next we create an S3 client:

Basically, the AmazonS3Client takes a credentials parameter and region. Here we are creating the credentials inline using an Incognito identity pool ID and a region endpoint. Once this is done, our S3 Client can be accessed through the public property “s3Client”. Make sure you don’t push your credentials to a public repository!

Now I will demonstrate how to list all the buckets. This is straight out of the SDK documentation:

Above we use the “List Buckets Async” method of the s3Client object. This method receives a List Bucket Request (created inline here), and a lambda which will determine what should happen when the request returns.

All we are doing above in the lambda is checking to see if there was an error with the request. If there was we will print out an error:

If we got a valid response, we will print out the bucket names:

This will display all the bucket names on the console if everything has gone well.

Next let’s upload an object to S3:

Going over the code above, the first thing I do is open a file stream to the object I want to upload. Here I am going to upload a data file from my hard drive.

For path, remember that Unity gives us “Application.persistentDataPath” to access data we save in our Unity application. From there you can add any additional text you need to identify your object in the folder:

A note for the above: awsCase.caseId is a class and property I created for this example.

Next we create a Post Object Request:

Above note that stream is the file stream we opened earlier.

Lastly, we make the post:

The above code is almost identical to the code that lists all buckets. The only change is that we use the “Post Object Async” method and pass it our request we created above. It also receives a lambda, which, in this case, will print out a success message if successful and an error message if there is an exception.

That’s all that is required for posting objects to AWS S3. You may need to set an IAM access role for your bucket or check other security settings on it if your request is blocked.

--

--

Jon Jenkins

A Unity Developer, interested in all things Unity.