Read data from the Amazon Cloud
Objective: Read a binary file from the AWS S3 and convert to useable format.
The Search_Panel allows the user to search for a case after entering data in the InputField caseNumberInput. Upon pressing the Search_Button the ProcessInfo() is called via the button’s on click() method. The ProcessInfo() method checks if a caseNumberInput has been entered, and if so, calls the GetObjectS3() method on the AWSManager, passing the caseNumberInput and an Action on what to do OnComplete to the method. The code is defined by () => {code is here}, i.e. open the next panel and close this panel.
The System.Runtime.Serialization.Formatters.Binary namespace is required by the GetObjectS3() method, and needs two string variables target and an AWS S3 bucket, where the data is stored. The target file name is constructed using the caseNumberInput from the SearchPanel.
The S3Client is used to open a connection to Amazon’s AWS S3, and described in a previous article. This client is used look for the target file in the bucket on S3, and returns responseObject, which is defined following the => or lambda expression. If the responseObject.Exception is null, i.e. no errors, then the bool caseFound is set based upon responseObject.Response.S3Objects.Any which checks each obj to see if obj.Key matches the target string. Any is part of the System.Linq namespace.
If the responseObject.Response.ResponseStream is not null then the target file was found, else the case was not found.
If the caseFound is true then the S3Client.GetObjectAsyn is used to get the object. If the ResponseStream is not null then it is read in a two step process. First a StreamReader, reader, is created from the ResponseStream. Next a local MemoryStream, memory, is opened. The reader then steps through each byte, until all bytes are read with a maximum size of buffer, writing each byteRead to memory. The byte[] array, data, is then assigned from memory once completed. The using command will automatically close memory once finished.
To convert the binary back into a Case, useable in Unity, a new local MemoryStream, also called memory, is created using the data byte array just created. A BinaryFormater, bf, is used to Deserialize this memory back into the Case class downloadedCase. This downloadedCase can now be read in Unity. The downloadedCase is used to set the UIManager’s activeCase. Finally, the OnComplete() action is executed activating the Select_Panel and inactivating the this Search_Panel.
It is critical to use S3CLient.ListObjectsAsync to detect the presence of the target, before getting the file. If the presence of the target is not confirmed before the S3Client.GetObjectAsync a memory leak will occur and Unity will lock up.
Saving and retrieving binary is very efficient, but it does take more code to convert back into a human readable format.