Uploading and downloading from AWS S3 in Unity
Let’s go over how to upload and download data from AWS S3 in Unity
This article go over how to upload and downloaded the data from this article. I showed how to get started with S3 in this article.
First, open up the AWSManager script and add the function called UploadToS3. Create a new FileStream and pass in the file path, set FileMode to open, FileAcess to read and write and FileShare also to read and write:
Create the S3 PostObjectRequest using the name of the bucket, set the file name, add the file stream, set access level and also set the region:
The last part is to submit the request. If successful call LoadScene to get back to the beginning:
Open the UIManager script and call the UploadToS3 function from the submit function:
To download from S3 go back to the AWSManager script and add the using directives System.Linq and System.Runtime.Serialization.Formatters.Binary:
Add the function LoadFromS3. Add a local string called target and a ListObjectsReqest:
Downloading the data takes few more steps than to upload. Similar to the upload you start with submitting the request. Using linq we check if a matching object is found:
If object is found we will request that object from the S3 storage:
The tricky part with downloading from S3 storage is that it is held in memory and not stored on your device. For us to make use of the data we first need to temporarily store this information in a byte array:
Once we have the data we can deserialize it and add it back to our data class:
Lastly, call the LoadFromS3 function:
Good luck!