

이제 AWS Mobile SDK for Unity가에 포함됩니다 AWS SDK for .NET. 이 가이드에서는 Mobile SDK for Unity의 아카이브 버전을 참조합니다. 자세한 내용은 [ AWS Mobile SDK for Unity란 무엇입니까?](what-is-unity-plugin.md) 섹션을 참조하세요.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# Amazon Simple Storage Service(S3)
<a name="s3"></a>

Amazon Simple Storage Service(S3)는 개발자와 IT 팀에 안전하고 내구성과 확장성이 뛰어난 객체 스토리지를 제공합니다. Unity 개발자는 S3를 활용하여 게임에 사용되는 자산을 동적으로 로드할 수 있습니다. 그러면 게임을 앱 스토어에서 처음 다운로드하는 속도를 높일 수 있습니다.

S3에 대한 자세한 내용은 [Amazon S3](https://aws.amazon.com/s3/)를 참조하십시오.

AWS S3 리전 가용성에 대한 자세한 내용은 [AWS 서비스 리전 가용성](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)을 참조하세요.

**참고**  
이 문서의 일부 샘플에서는 ResultText라는 텍스트 상자 변수를 사용하여 추적 출력을 표시하는 것으로 가정합니다.

## S3 버킷 생성 및 구성
<a name="create-and-configure-an-s3-bucket"></a>

Amazon S3는 리소스를 Amazon S3 버킷(특정 [리전](https://docs.aws.amazon.com/general/latest/gr/rande.html)에 위치하는 클라우드 스토리지 컨테이너)에 저장합니다. 각 Amazon S3 버킷은 전역적으로 고유한 이름을 가져야 합니다. [Amazon S3 콘솔](https://console.aws.amazon.com/s3/)을 사용하여 버킷을 생성할 수 있습니다.

### S3 버킷 생성
<a name="create-an-s3-bucket"></a>

1. [Amazon S3 콘솔](https://console.aws.amazon.com/s3/)에 로그인하고 **버킷 만들기**를 클릭합니다.

1. 버킷 이름을 입력하고 리전을 선택한 다음 **생성**을 클릭합니다.

### S3에 대한 권한 설정
<a name="set-permissions-for-s3"></a>

기본 IAM 역할 정책은 애플리케이션에 Amazon Mobile Analytics 및 Amazon Cognito Sync에 대한 액세스 권한을 부여합니다. Cognito 자격 증명 풀이 Amazon S3에 액세스할 수 있으려면 자격 증명 풀의 역할을 수정해야 합니다.

1. [Identity and Access Management 콘솔](https://console.aws.amazon.com/iam/home)로 이동하여 왼쪽 창에서 **역할**을 클릭합니다.

1. 검색 상자에 자격 증명 풀 이름을 입력합니다. 인증된 사용자와 인증되지 않은 사용자에 대해 하나씩 2개의 역할이 나열됩니다.

1. 인증되지 않은 사용자의 역할을 클릭합니다(자격 증명 풀 이름에 unauth가 추가됨).

1. **역할 정책 생성**을 클릭하고 **정책 생성기**를 선택한 다음 **선택**을 클릭합니다.

1. **권한 편집** 페이지에서 다음 이미지에 표시된 설정을 입력합니다. Amazon 리소스 이름(ARN)은 사용자의 것으로 바꿔야 합니다. S3 버킷의 ARN은 `arn:aws:s3:::examplebucket/*`과 비슷하며, 버킷이 위치하는 리전과 버킷의 이름으로 구성됩니다. 아래 표시된 설정은 자격 증명 풀에 지정된 버킷의 모든 작업에 대한 전체 액세스를 부여합니다.  
![\[Edit Permissions interface for AWS policy creation, showing options for Amazon S3 access control.\]](http://docs.aws.amazon.com/ko_kr/mobile/sdkforunity/developerguide/images/edit-permissions.png)

1. **설명문 추가** 버튼을 클릭한 다음 **다음 단계**를 클릭합니다.

1. 마법사가 앞서 생성한 구성을 표시합니다. **정책 적용**을 클릭합니다.

S3 액세스 권한 부여에 대한 자세한 내용은 [Amazon S3 버킷에 대한 액세스 권한 부여](http://blogs.aws.amazon.com/security/post/Tx3VRSWZ6B3SHAV/Writing-IAM-Policies-How-to-grant-access-to-an-Amazon-S3-bucket)를 참조하세요.

### 콘솔에서 파일 업로드
<a name="upload-files-from-the-console"></a>

버킷에 테스트 파일을 업로드하는 방법:

1. S3 콘솔의 해당 버킷 보기에서 **업로드**를 클릭합니다.

1. **파일 추가**를 클릭하고 업로드할 테스트 파일을 선택합니다. 이 자습서에서는 `myImage.jpg`라는 이미지를 업로드하는 것으로 가정합니다.

1. 테스트 이미지를 선택한 상태에서 **업로드 시작**을 클릭합니다.

### (선택 사항) S3 요청의 서명 버전을 구성합니다.
<a name="optional-configure-the-signature-version-for-s3-requests"></a>

Amazon S3와의 모든 상호 작용은 인증을 거치거나 익명으로 할 수 있습니다. AWS는 서명 버전 4 또는 서명 버전 2 알고리즘을 사용해 서비스를 직접적으로 호출하는 것을 인증합니다.

2014년 1월 이후 생성된 모든 새 AWS 리전은 서명 버전 4만 지원합니다. 하지만 그 이전의 리전은 계속해서 서명 버전 4 및 서명 버전 2 요청을 지원합니다.

버킷이 [이 페이지](https://docs.aws.amazon.com/general/latest/gr/signature-version-2.html)에 나열된 대로 서명 버전 2 요청을 지원하지 않는 리전 중 하나에 위치하는 경우 AWSConfigsS3.UseSignatureVersion4 속성을 "true"로 설정해야 합니다.

AWS 서명 버전에 대한 자세한 내용은 [요청 인증(AWS 서명 버전 4)](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html)을 참조하세요.

## Amazon S3 클라이언트 생성
<a name="create-the-amazon-s3-client"></a>

Amazon S3를 사용하려면 먼저 앞서 생성한 CognitoAWSCredentials 인스턴스를 참조하는 AmazonS3Client 인스턴스를 생성해야 합니다.

```
AmazonS3Client S3Client = new AmazonS3Client (credentials);
```

`AmazonS3Client` 클래스는 상위 수준 S3 API의 진입점입니다.

## 버킷 목록 생성
<a name="list-buckets"></a>

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";
     }
 });
```

## 객체 목록 생성
<a name="list-objects"></a>

특정 버킷의 모든 객체를 나열하려면 다음 샘플 코드와 같이 `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";
    }
});
```

## 객체 다운로드
<a name="download-an-object"></a>

객체를 다운로드하려면 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 인스턴스는 선택 사항입니다. 지정할 경우 콜백이 기본 스레드에서 실행될지 여부를 결정합니다.

## 객체 업로드
<a name="upload-an-object"></a>

객체를 업로드하려면 객체를 스트림에 작성하고 새 PostObjectRequest를 생성한 다음 키, 버킷 이름 및 스트림 데이터를 지정합니다.

AWS SDK for Unity는 HTTP PUT 작업을 지원하지 않는 WWW HTTP 클라이언트를 사용합니다. S3 버킷에 객체를 업로드하려면 아래와 같이 S3의 Browser 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());
        }
    });
}
```