Data Communication with AWS: Part 4- App Dev Series 10

Objective: upload file to S3 bucket.

Photo by Chris Ried on Unsplash

Now we have prepared everything needed to upload our file to S3, let’s get to it.

Method to upload

According to the documentation of AWS SDK for Unity, we need to declare a file path to S3 bucket, then set the information of file-to-upload, and we can “Post” this file to check the result.
We will do these part one by one.

Create a public method in AWSManager script, which will be called when user would like to submit the data.
Inside this method, first we need to create a FileStream to locate the file to upload. And use the pass in string as file path, which we will send the path from UIManager.

Then use PostObjectRequest to gather the information of file to send.
Within this, it would need to require some key info:

  • Bucket: the bucket name of your S3.
  • Key: the file name.
  • InputStream: the direction of file.
  • CannedACL: access permission of this file in S3 bucket.
  • Region: the region you set for this bucket.

Using these info as a new PostObjectRequest().

Now we can request to client to upload this object by using PostObjectAsync.
We will need an if statement to check the exception of response object. If there is no exception, then the object has successfully uploaded.

That’s all for this method to upload.
Now we just need to call this method and the object would be uploaded to S3.

File ID from app before uploading.
Uploaded object on S3 bucket.

--

--

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