AWS S3 & Unity

Ali Emre Onur
5 min readJun 17, 2021

Now that we have successfuly saved the input data, it is time to upload it using AWS. S3 stands for Simple Storage Services, which is the service we are going to use to store the user data.

Before beginning, I shall note that I will be giving permission to unauthorized access. Go to aws.amazon.com and login to your AWS account. Click on Account — AWS Management Console and then Incognito. Then click on Manage Identity Pools and select the application that you have created before on the screen. Select “Edit Identity Pool” on the right hand side of the screen and you will get your Identity pool ID.

For detailed information and guideline on using S3, you can simply follow the link below:

https://docs.aws.amazon.com/AmazonS3/latest/userguide/GetStartedWithS3.html

Secondly, go back to AWS Management Console and type S3 to the search bar to access to the S3 platform. It will tell you that you do not have any buckets. Thus, we need to Create a bucket by clicking the button. Now, we can create folders after creating a bucket. Now, we can upload the dat files that we generate within the application.

Create an empty game object with the name AWSManager and drag the AWSManager onto it. Make sure to add the necessary namespaces to the script.

Now that we have established a connection with AWS, it is time to work with the buckets. You should be careful about the region that you are working on. If you set your script to an incorrect region, it will provide an error.

public string S3Region = RegionEndpoint.EUCentral1.SystemName;private RegionEndpoint _S3Region{get{return RegionEndpoint.GetBySystemName(S3Region);}}private void Awake(){UnityInitializer.AttachToGameObject(this.gameObject);AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;CognitoAWSCredentials credentials = new CognitoAWSCredentials(“youridhere”, RegionEndpoint.EUCentral1);AmazonS3Client S3Client = new AmazonS3Client(credentials, _S3Region);S3Client.ListBucketsAsync(new ListBucketsRequest(), (responseObject) =>{if (responseObject.Exception == null){responseObject.Response.Buckets.ForEach((s3b) =>{Debug.Log(“Bucket Name : “ + s3b.BucketName);});}else{Debug.Log(“AWS Error” + responseObject.Exception);}});}

With the update above, now we will get an “Access Denied” Error.

From AWS Management Console, type IAM to access the Identity and Acces Management service. On the left hand side, click on Roles. Select the “Cognito_ServiceAdjustmentAppUnauth_Role” and copy Role ARN.

Now, get back to the S3 Management screen, select the bucket you have created, click on Permissions. Slide the page down a little bit and you will observe Bucket Policy — click on Edit.

Now, click on Policy Generator. Select the Type of Policy to “S3 Bucket Policy” and paste the Role ARN to Principal field that you have copied from the IAM console.

On the actions, check the “All Actions” box. As the Amazon Resource Name (ARN) paste down the bucket ARN and add a “/*” to the end of it (without the brackets).

Bucket ARN

Click on add Statement, and then Generate Policy and a JSON Document will Pop-up. Copy the code in it and paste it into the field on the Edit bucket policy screen.

And lastly, click on Save.

These steps still failed me so here’s the way to solve it:

Click on Policies (On the S3 Management Console) and click on Create Policy. Select Amazon S3 az the service. Select All Actions (checkbox) and Resources (checkbox). Click on next, give it a name and save it. Switch back to Roles, select “Cognito_ServiceApp2Unath_Role”, click “Attach policies” and select the policy with the name that you have just created and add it.

If Unity still gives out an error, make sure to also add the policy to “Cognito_ServiceAdjustmentAppUnauth_Role”.

Lastly, to upload to the AWS, we need to add an Upload method and call it from the UIManager once we hit the submit button. Using the guidelines in the S3 website, we can apply the required fields in our code. If we successfully upload the data, we just reload the scene.

UIManager Script SubmitButton method

It works for me now.

If you check your S3 account and select the bucket you have created and using, you will realize that the file has been uploaded to the server.

Getting List of All Items in S3:

Before downloading the case that is searched by the user, we need to gather the list of cases in the server. And then, we need to search for the case according to the users demand. If there’s a match, next scene needs to be loaded. Just like we serialized the data in order to store it, we have deserialized it while calling it back — so that it will be readable with human eyes — rather than the machine.

AWSManager script

On the SearchPanel, we have used a Callback system

Works like a charm!

--

--