Nerd For Tech
Published in

Nerd For Tech

Retrieving & Decoding Stored Data from AWS S3 in Unity

How to Dump Out Your Bucket

I know my data is in here somewhere…

Yesterday we covered converting data into a binary file and uploading it to an S3 bucket. Today we’ll be doing the reverse: finding stored data, decoding it, and making it accessible to the user.

Let’s get to work.

Getting the Gist and Making a List

The task here is to let the user look up submitted claim data in a mobile insurance claim reporting app.

Amazon has a whole host of useful C# methods Unity can access to handle the work of retrieving data. The full details can be found here; we’ll be modifying their example heavily to suit our use case.

We don’t want to download the whole bucket — just the specific file we’re looking for. The first thing we need to do is get a list of items in our S3 Bucket. Then we can search the list for the item we need, and download it. Then we have to convert it from a binary file into data we can populate into an instance of the Case class.

Here’s the pseudo code for a GetList method, which accepts a string for the caseNumber and an optional onComplete action method:

Declare a new instance of the ListObjectsRequest class called ‘request’
Call the S3Client ListObjectsAsync method, passing in the request and an inline delegate called ‘responseObject’:
… if there are no exceptions
……declare a bool that runs a LINQ inquiry on the list of objects in the bucket
……if the bool comes up true (the case is found)

……[data decoding goes here]

……else if the case is not found, log it
… else if there are exceptions, log them

At this point we’ve got a list of items and we’ve zeroed in on what we want. Next, we need to make the data useful to us.

Decoding the Data

Continuing on from where we left off with our pseudo code, we need to introduce some memory streams to handle our data:

Call the S3Client GetObjectAsync method, passing in bucket, the target file, and another inline delegate called ‘responseObject’:
…Check if the response is null — if not:
……create a byte array to store data from file
……using a new instance of the StreamReader class, pass in the response stream:
………using a new instance of the MemoryStream class:
…………create a byte array (size 512) to act as a buffer for the data
…………create a variable to track read bytes
…………while the bytes read are greater than 0
……………call the MemoryStream Write method, passing in the buffer and the byte tracker
…………pass the memory stream into the data byte array
……using another new instance of the MemoryStream class, pass in the data byte array:
………declare a new instance of the BinaryFormatter class
………declare a new instance of the Case class and equate it to a call on the BinaryFormatter’s Deserialize method, passing in the memory stream.
………equate the UI Manager’s active case to the downloaded case
………if the class calling this method has an onComplete action, run it

Wow. That’s a lot of nested operations. To briefly recap: we ask AWS for a list of items, we query the list for the item we’re searching for, we create a container for the data, open up a stream reader, which opens up a memory stream, which downloads and buffers the incoming data before putting it into our container. Finally, we deserialize the data with a Binary Formatter and load the result into our active case.

Whew.

Here’s the C# for the GetList method:

Let’s see how well it works:

Splendid.

That’s everything I have for today. Tomorrow I’ll be rounding out the navigation features of the app by implementing the Command Pattern. Be sure to check it out!

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

7 Scrum pitfalls to avoid

Momentum Football Bets

Details about well- http://ift.tt/2jhb9xb

Sector and Industry Analysis — Gamestonk Terminal

Git Interactive Rebase — The Gem You Have To Know About

CS373 Spring 2022: Blog #4

Look for these habits when you hire developers

Using NFTs To Increase Customer Engagement For Businesses

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Micha Davis

Micha Davis

Unity Developer / Game Developer / Artist / Problem Solver

More from Medium

Aws setup for Unity

Load data into Amazon Cloud

Using AWS S3 with Unity

Speed Up Your CI With Docker (Travis-CI)