Data Communication with AWS: Part 5- App Dev Series 11

S.J. Jason Liu
4 min readSep 13, 2021

Objective: download file from S3 and convert it to make Unity can read it.

The last part of using AWS is to download the file from our bucket and convert it into the format that Unity would be able to read, then we can show the information from that file.

Concept

To download the specific file from S3, we will doing the following steps:

  • Getting the file list from bucket
  • Locate the file we want(input): compare the file we want(input) to all files from bucket
  • Read the file and convert it into byte array
  • Convert the byte array to case object that can be read by Unity

It would be kind of complex and we will walk through this step by step.

Getting file list and locate the file

First we need to create a public method to pass in the file we want to search.
Within that method, declare a target file name to search.

Next, using ListObjectsRequest to list the object from our bucket.
After we get the list, we can looking for the list to compare with the target file by using ListObjectsAsync.

In ListObjectsAsync, we use “requestObj” as Lambda Expressions to check if the located file is what we want.
Then create an if statement to check if there is any exception, if not, and we can compare the target file name to bucket list.

If the bool returns true, that means we have located the file we want, then we can get that file and proceed.
And we need to use GetObjectAsync to grab the file.

Read the file and convert it

Now would be the core part of these article. Since we get the object(file) from bucket, we can not just download and import it into Unity. We need to turn the format of this object into byte array. However, we need to use MemoryStream to help with this converting.

First, let’s check if the object we located is null. If it is not null, let’s declare a byte array variable for a later use, then using StreamReader to read the located object.

With the read file, using MemoryStream to populate the byte array.
In here, we would set a maximum array range, then set an integer byte format to store the data from StreamReader with a while loop, which means we would cover all byte data from that file.

Once we storage every byte data, write it back to MemoryStream, and set is as the byte array we created.

Convert byte array back to case file

Now we can use another MemoryStream to deserialize the byte array back to case file that Unity would read.
Using BinaryFormatter as what we been used to turn the case file into byte array, but with deserialize this time.

The “downloadedCase” is the file we located and downloaded from S3 bucket.
You can use it to reveal in your app now.

That’s all for using AWS S3 service in Unity app.

--

--

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.