How to Save a Binary File to AWS

Saving a binary file to Amazon Web Services for later retrieval (Part 2)

Ryan Sweigart
AWS in Plain English

--

Continuing from our previous article (https://lordkakabel.medium.com/saving-a-file-to-aws-26adf62e79ef), we’ll now load the file we saved to Amazon Web Services (AWS). We’ll do this by calling our GetObjectList method. This method takes-in the user-inputted case number and an Action that will trigger (if supplied).

We’ll take the user’s case number and validate it to create a proper file name. Then we’ll create a request for a list of objects in our AWS bucket.

Next we’ll cycle through the list of objects. If we get an exception, we’ll report it and won’t be able to load the file.

If we don’t get an exception, we’ll look to see if any of the objects in the list match our case file name. If not, we’ll report that.

If a match was found, we’ll get the object. Again, if an exception occurs, we’ll report it.

Now that we have the object, we’ll prepare an array of bytes to hold the data. Then we’ll use a StreamReader read the response data, then access the MemoryStream.

Now we’ll create another byte array called buffer that is big enough to hold all the data in our file (512 bytes is plenty for my project). We’ll then read through the file, writing the file contents to our memory stream. Then we’ll put the memory stream into our array of bytes.

We’ll take our array of bytes and assign it to a new memory stream. We’ll create a new BinaryFormatter. We’ll use that binary formatter to Deserialize the memory stream and, in my project, store it as a Case object. We’ll tell the UIManager that this new object is active one we’d like to work with. Finally, if an Action was passed-in when this method was called, we’ll trigger it.

Our case number file was found and loaded!

More content at plainenglish.io

--

--