Amazon Simple Storage Service (S3) - 适用于 Unity 的 AWS Mobile SDK

AWS SDK for .NET 现在随附适用于 Unity 的 AWS Mobile SDK。本指南引用适用于 Unity 的 Mobile SDK 的存档版本。有关更多信息,请参阅什么是适用于 Unity 的 AWS Mobile SDK?

Amazon Simple Storage Service (S3)

Amazon Simple Storage Service (Amazon S3) 为开发人员和 IT 团队提供安全、持久、高度可扩展的对象存储。Unity 开发人员可以利用 S3 来动态加载其游戏所使用的资产。这使得从一开始就能更快地从应用程序商店下载游戏。

有关 S3 的更多信息,请参阅 Amazon S3

有关 AWS S3 区域可用性的信息,请参阅 AWS 服务区域可用性

注意

本文档中的一些示例使用文本框变量 ResultText 来显示跟踪输出。

创建和配置 S3 存储桶

Amazon S3 可将您的资源存储到 Amazon S3 存储桶 (存在于某个特定区域的云存储容器)。每个 Amazon S3 存储桶必须具有一个全局唯一名称。您可以使用 Amazon S3 控制台创建存储桶。

创建 S3 存储桶

  1. 登录 Amazon S3 控制台,然后单击 Create Bucket

  2. 输入存储桶的名称,选择一个区域,然后单击 Create

设置 S3 权限

默认 IAM 角色策略会授予您的应用程序访问 Amazon Mobile Analytics 和 Amazon Cognito Sync 的权限。为了让您的 Cognito 身份池能够访问 Amazon S3,您必须修改身份池的角色。

  1. 转至 Identity and Access Management Console,然后单击左窗格中的 Roles

  2. 在搜索框中键入您的身份池名称。将列出两个角色:一个用于未经身份验证的用户,另一个用于经过身份验证的用户。

  3. 单击用于未经过身份验证的用户的角色 (身份池名称后附加有“unauth”)。

  4. 单击 Create Role Policy,选择 Policy Generator,然后单击 Select

  5. 编辑权限页面上,输入下图所示的设置,用您自己的资源名称替换 Amazon 资源名称 (ARN)。S3 存储桶的 ARN 类似 arn:aws:s3:::examplebucket/*,由存储桶所在的区域和存储桶的名称构成。下面显示的设置将赋予您的身份池对指定存储桶执行所有操作的完全访问权限。

    Edit Permissions interface for AWS policy creation, showing options for Amazon S3 access control.
  1. 单击 Add Statement 按钮,然后单击 Next Step

  2. 向导将向您显示生成的配置。单击应用策略

有关授予访问 S3 的权限的更多信息,请参阅授予访问 Amazon S3 存储桶的权限

从控制台上传文件

将测试文件上传到您的存储桶:

  1. 在 S3 控制台上的存储桶视图中,单击 Upload

  2. 单击 Add Files,然后选择一个要上传的测试文件。在本教程中,我们假设您上传一个名为 myImage.jpg 的图像。

  3. 选择测试图像后,单击 Start Upload

(可选) 配置针对 S3 请求的签名版本

与 Amazon S3 的每一次交互都是经身份验证的或匿名的。AWS 使用签名版本 4 或签名版本 2 算法来对服务调用进行身份验证。

2014 年 1 月之后创建的所有新的 AWS 区域仅支持签名版本 4。但是,许多较旧的区域仍支持签名版本 4 和签名版本 2 请求。

如果您的存储桶位于不支持签名版本 2 请求的区域之一,如本页中所列,则您必须将 AWSConfigsS3.UseSignatureVersion4 属性设置为“true”。

有关 AWS 签名版本的更多信息,请参阅对请求进行身份验证(AWS 签名版本 4)

创建 Amazon S3 客户端

要使用 Amazon S3,我们首先需要创建一个 AmazonS3Client 实例,用于引用您之前创建的 CognitoAWSCredentials 实例:

AmazonS3Client S3Client = new AmazonS3Client (credentials);

AmazonS3Client 类是高级 S3 API 的入口点。

列出存储桶

要列出 AWS 账户中的存储桶,请调用 AmazonS3Client.ListBucketsAsync 方法,如以下示例代码中所示:

// ResultText is a label used for displaying status information ResultText.text = "Fetching all the Buckets"; Client.ListBucketsAsync(new ListBucketsRequest(), (responseObject) => { ResultText.text += "\n"; if (responseObject.Exception == null) { ResultText.text += "Got Response \nPrinting now \n"; responseObject.Response.Buckets.ForEach((s3b) => { ResultText.text += string.Format("bucket = {0}, created date = {1} \n", s3b.BucketName, s3b.CreationDate); }); } else { ResultText.text += "Got Exception \n"; } });

列出对象

要列出一个存储桶中的所有对象,请调用 AmazonS3Client.ListObjectsAsync 方法,如以下示例代码中所示:

// ResultText is a label used for displaying status information ResultText.text = "Fetching all the Objects from " + S3BucketName; var request = new ListObjectsRequest() { BucketName = S3BucketName }; Client.ListObjectsAsync(request, (responseObject) => { ResultText.text += "\n"; if (responseObject.Exception == null) { ResultText.text += "Got Response \nPrinting now \n"; responseObject.Response.S3Objects.ForEach((o) => { ResultText.text += string.Format("{0}\n", o.Key); }); } else { ResultText.text += "Got Exception \n"; } });

下载对象

要下载对象,请创建一个 GetObjectRequest,指定存储桶名称和键,并将该对象传递给对 Client.GetObjectAsync 的调用:

private void GetObject() { ResultText.text = string.Format("fetching {0} from bucket {1}", SampleFileName, S3BucketName); Client.GetObjectAsync(S3BucketName, SampleFileName, (responseObj) => { string data = null; var response = responseObj.Response; if (response.ResponseStream != null) { using (StreamReader reader = new StreamReader(response.ResponseStream)) { data = reader.ReadToEnd(); } ResultText.text += "\n"; ResultText.text += data; } }); }

GetObjectAsync 采用 GetObjectRequest 的实例、回调和 AsyncOptions 实例。回调的类型必须为:AmazonServiceCallback<GetObjectRequest, GetObjectResponse>。AsyncOptions 实例是可选的。如果指定,它将决定是否将在主线程上运行回调。

上传对象

要上传对象,请将对象写入一个流,创建一个新的 PostObjectRequest,并指定键、存储桶名称和流数据。

适用于 Unity 的 AWS SDK 使用不支持 HTTP PUT 操作的 WWW HTTP 客户端。要想将对象上传到您的 S3 存储桶,需要使用 S3 的浏览器 Post,如下所示。

public void PostObject(string fileName) { ResultText.text = "Retrieving the file"; var stream = new FileStream(Application.persistentDataPath + Path.DirectorySeparatorChar + fileName, FileMode.Open, FileAccess.Read, FileShare.Read); ResultText.text += "\nCreating request object"; var request = new PostObjectRequest() { Bucket = S3BucketName, Key = fileName, InputStream = stream, CannedACL = S3CannedACL.Private }; ResultText.text += "\nMaking HTTP post call"; Client.PostObjectAsync(request, (responseObj) => { if (responseObj.Exception == null) { ResultText.text += string.Format("\nobject {0} posted to bucket {1}", responseObj.Request.Key, responseObj.Request.Bucket); } else { ResultText.text += "\nException while posting the result object"; ResultText.text += string.Format("\n receieved error {0}", responseObj.Response.HttpStatusCode.ToString()); } }); }