Learn - amazon s3

A

Preparations

Before using the plugin an AWS account must be created and the security credentials must be retrieved.
1

AWS Sign up

Everything you need to know about AWS can be found here. Before you start using this plugin sign up and go through the "Getting started with AWS in under 15 minutes"
2

Getting Your Access Key ID and Secret Access Key

There is basically two ways to get the access key ID and secret access key. Either through the Security Credentials page for your AWS user or create a user through the IAM Manager and get the keys for that specific user. Adding a new user for your game (e.g. MyAwesomeGameUser) is recommended rather than using the keys for your main AWS user.

Goto http://docs.aws.amazon.com/general/latest/gr/getting-aws-sec-creds.html to find out more on how to setup and handle security credentials. 

Important, you should also read the IAM Best Practices
B

Amazon S3 Utility

The AmazonS3Utility class makes it easier to use AmazonS3. The class is created as a state machine and is designed with the idea that you shouldn't be able to use the methods unless it's initialized and ready to use.
1

Adding the Utility as a Component

Amazon_1

Adding the Utility as a component to a new game object.

2

Setting up a test class

For the purpose of this tutorial, we'll create a new script called S3TestingScript and attach it to the same game object as the AmazonS3Utility component. We'll start by creating a reference to the utility as one of the members along with your use account settings for AWS.
public class S3TestingScript : MonoBehaviour
{
	public string		m_accessKey			= "INSERT_ACCESS_KEY_HERE";
	public string		m_secretKey			= "INSERT_SECRET_KEY_HERE";
	public EAWSRegion	m_region			= EAWSRegion.USEast1;
	AmazonS3Utility		m_s3Service			= null;

	void Awake()
	{
		//This only works if the utility is attached to the same gameobject as
		//this script. Otherwise you need to reference it either publicly or through a method.
		m_s3Service = GetComponent<AmazonS3Utility>();
	}
}
3

Initializing the utility class

Pass in the access key, secret key and the operating region to the initialize method to initialize the Amazon Utility. All the methods in our AWS library are operating asynchronously, so before you're using any other method you need to wait for the IsInitialized property to be set to true.
void OnGUI()
{
	if(!m_s3Service.IsInitialized)
	{
		if(GUILayout.Button("Initialize", GUILayout.MinHeight(Screen.height * 0.2f), GUILayout.Width(Screen.width * 0.45f)))
			m_s3Service.Initialize(m_accessKey, m_secretKey, m_region);
	}
}
C

Code Examples

1

Creating a bucket

void OnGUI()
{
	if(!m_s3Service.IsInitialized)
	{
		if(GUILayout.Button("Initialize", GUILayout.MinHeight(Screen.height * 0.2f), GUILayout.Width(Screen.width * 0.45f)))
			m_s3Service.Initialize(m_accessKey, m_secretKey, m_region);
	}
	else
	{
		if(GUILayout.Button("Create bucket" , GUILayout.MinHeight(Screen.height * 0.1f), GUILayout.Width(Screen.width * 0.45f)))
			m_s3Service.CreateBucket("MY_UNIQUE_BUCKET_NAME", OnBucketCreated);
	}
}

void OnBucketCreated(bool success, string bucketName)
{
	if(success)
		Debug.Log("Successfully created bucket with name: " + bucketName);
	else
		Debug.LogError("Failed to create bucket with name: " + bucketName);
}
2

Downloading a file

void OnGUI()
{
	if(!m_s3Service.IsInitialized)
	{
		if(GUILayout.Button("Initialize", GUILayout.MinHeight(Screen.height * 0.2f), GUILayout.Width(Screen.width * 0.45f)))
			m_s3Service.Initialize(m_accessKey, m_secretKey, m_region);
	}
	else
	{
		if(GUILayout.Button("Download File" , GUILayout.MinHeight(Screen.height * 0.1f), GUILayout.Width(Screen.width * 0.45f)))
			m_s3Service.DownloadFile("MY_UNIQUE_BUCKET_NAME", "MY_FILE_KEY", "FILE_DESTINATION", OnDownloadedFile);
	}
}

void OnDownloadedFile(bool success, string key)
{
	if(success)
		Debug.Log("Successfully downloaded " + key + "!");
	else
		Debug.Log("Failed to download " + key + "!");
}
3

Downloading a directory

void OnGUI()
{
	if(!m_s3Service.IsInitialized)
	{
		if(GUILayout.Button("Initialize", GUILayout.MinHeight(Screen.height * 0.2f), GUILayout.Width(Screen.width * 0.45f)))
			m_s3Service.Initialize(m_accessKey, m_secretKey, m_region);
	}
	else
	{
		if(GUILayout.Button("Download Directory" , GUILayout.MinHeight(Screen.height * 0.1f), GUILayout.Width(Screen.width * 0.45f)))
			m_s3Service.DownloadDirectory("MY_UNIQUE_BUCKET_NAME", "MY_DIRECTORY_KEY", "DIRECTORY_DESTINATION", true, OnDownloadedDirectory);
	}
}

void OnDownloadedDirectory(bool success, string key)
{
	if(success)
		Debug.Log("Successfully downloaded directory: " + key + "!");
	else
		Debug.Log("Failed to download directory: " + key + "!");
}
D

Using Amazon S3 in editor scripts

You can't use the AmazonS3Utility class if you want to use Amazon S3 in an editor extension or an editor script. The utility class relies heavily on Update() and the time structure used at runtime.

To use the methods in the editor, we recommend you to use our .NET interface class called NETAWSS3. One thing to take note of before using this class is that you will have to keep track of the initialization, completion as well as the order of method executions by yourself.
public class MyEditorClass
{
	NETAWSS3 amazonS3 = null;

	public MyEditorClass()
	{
		amazonS3 = new NETAWSS3();
	}

	public void Initialize(string accessKey, string secretKey, EAWSRegion region)
	{
		amazonS3.Initialize(accessKey, secretKey, region, OnInitialize);
	}

	void OnInitialize(bool success)
	{
		if(success)
			Debug.Log("Amazon S3 Initialized Successfully!");
		else
			Debug.LogError("Failed to Initialize Amazon S3!");
	}
}
E

Encryption of Access & Secret Access keys