

AWS SDK for JavaScript v2가 지원 종료에 도달했습니다. [AWS SDK for JavaScript v3](https://docs.aws.amazon.com//sdk-for-javascript/v3/developer-guide/)로 마이그레이션하실 것을 권장합니다. 마이그레이션 방법에 대한 자세한 내용은 해당 [공지 사항](https://aws.amazon.com/blogs//developer/announcing-end-of-support-for-aws-sdk-for-javascript-v2/)을 참조하세요.

# Amazon S3 버킷을 정적 웹 호스트로 사용
<a name="s3-example-static-web-host"></a>

![\[JavaScript code example that applies to Node.js execution\]](http://docs.aws.amazon.com/ko_kr/sdk-for-javascript/v2/developer-guide/images/nodeicon.png)

**이 Node.js 코드 예제는 다음을 보여 줍니다.**
+ Amazon S3 버킷을 정적 웹 호스트로 설정하는 방법

## 시나리오
<a name="s3-example-static-web-host-scenario"></a>

이 예제에서는 일련의 Node.js 모듈을 사용하여 정적 웹 호스트 역할을 하는 버킷 중 하나를 구성합니다. Node.js 모듈은 SDK for JavaScript로 Amazon S3 클라이언트 클래스의 다음 메서드를 사용하여 선택한 Amazon S3 버킷을 구성합니다.
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getBucketWebsite-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getBucketWebsite-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putBucketWebsite-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putBucketWebsite-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteBucketWebsite-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteBucketWebsite-property)

Amazon S3 버킷을 정적 웹 호스트로 사용하는 방법에 대한 자세한 내용은 *Amazon Simple Storage Service 사용 설명서*의 [Amazon S3에서 정적 웹 사이트 호스팅](https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteHosting.html)을 참조하세요.

## 사전 필수 작업
<a name="s3-example-static-web-host-prerequisites"></a>

이 예제를 설정하고 실행하려면 먼저 이러한 작업들을 완료해야 합니다.
+ Node.js를 설치합니다. Node.js 설치에 대한 자세한 내용은 [Node.js 웹 사이트](https://nodejs.org)를 참조하세요.
+ 사용자 자격 증명을 사용하여 공유 구성 파일을 생성합니다. 공유 자격 증명 파일 제공에 대한 자세한 내용은 [공유 인증 자격 증명 파일에서 Node.js에 인증 자격 증명 로드](loading-node-credentials-shared.md) 섹션을 참조하세요.

## SDK 구성
<a name="s3-example-static-web-host-configure-sdk"></a>

글로벌 구성 객체를 생성한 후 코드에 대한 리전을 설정하여 SDK for JavaScript를 구성합니다. 이 예제에서 리전이 `us-west-2`로 설정되어 있습니다.

```
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the Region 
AWS.config.update({region: 'us-west-2'});
```

## 현재 버킷 웹 사이트 구성 검색
<a name="s3-example-static-web-host-get-website"></a>

파일 이름이 `s3_getbucketwebsite.js`인 Node.js 모듈을 생성합니다. 이 모듈은 원하는 웹 사이트 구성이 있는 버킷을 지정하는 단일 명령줄 인수를 가져옵니다. 위와 같이 SDK를 구성합니다.

`AWS.S3` 서비스 객체를 생성합니다. 버킷 목록에서 선택한 버킷에 대한 현재 버킷 웹 사이트 구성을 검색하는 함수를 생성합니다. 전달해야 하는 유일한 파라미터는 `getBucketWebsite` 메서드를 호출할 때 선택한 버킷의 이름입니다. 현재 버킷에 웹 사이트가 구성된 경우 해당 구성은 Amazon S3에서 콜백 함수에 전달되는 `data` 파라미터에 반환됩니다.

선택한 버킷에 웹 사이트 구성이 없는 경우 해당 정보는 `err` 파라미터에서 콜백 함수에 반환됩니다.

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create S3 service object
s3 = new AWS.S3({ apiVersion: "2006-03-01" });

var bucketParams = { Bucket: process.argv[2] };

// call S3 to retrieve the website configuration for selected bucket
s3.getBucketWebsite(bucketParams, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else if (data) {
    console.log("Success", data);
  }
});
```

예제를 실행하려면 명령줄에서 다음을 입력합니다.

```
node s3_getbucketwebsite.js BUCKET_NAME
```

이 샘플 코드는 [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_getbucketwebsite.js)에서 찾을 수 있습니다.

## 버킷 웹 사이트 구성 설정
<a name="s3-example-static-web-host-set-website"></a>

파일 이름이 `s3_setbucketwebsite.js`인 Node.js 모듈을 생성합니다. 위와 같이 SDK를 구성해야 합니다. `AWS.S3` 서비스 객체를 생성합니다.

버킷 웹 사이트 구성을 적용하는 함수를 생성합니다. 선택한 버킷은 이 구성을 사용하여 정적 웹 호스트의 역할을 수행할 수 있습니다. 웹 사이트 구성은 JSON에서 지정됩니다. 먼저, 오류 문서를 식별하는 `Key` 값과 인덱스 문서를 식별하는 `Suffix` 값을 제외하고 웹 사이트 구성을 지정하기 위한 모든 값을 포함하는 JSON 객체를 생성합니다.

텍스트 입력 요소의 값을 JSON 객체에 삽입합니다. 버킷의 이름과 JSON 웹 사이트 구성을 포함하여 `putBucketWebsite` 메서드를 위한 파라미터를 준비합니다.

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create S3 service object
s3 = new AWS.S3({ apiVersion: "2006-03-01" });

// Create JSON for putBucketWebsite parameters
var staticHostParams = {
  Bucket: "",
  WebsiteConfiguration: {
    ErrorDocument: {
      Key: "",
    },
    IndexDocument: {
      Suffix: "",
    },
  },
};

// Insert specified bucket name and index and error documents into params JSON
// from command line arguments
staticHostParams.Bucket = process.argv[2];
staticHostParams.WebsiteConfiguration.IndexDocument.Suffix = process.argv[3];
staticHostParams.WebsiteConfiguration.ErrorDocument.Key = process.argv[4];

// set the new website configuration on the selected bucket
s3.putBucketWebsite(staticHostParams, function (err, data) {
  if (err) {
    // display error message
    console.log("Error", err);
  } else {
    // update the displayed website configuration for the selected bucket
    console.log("Success", data);
  }
});
```

예제를 실행하려면 명령줄에서 다음을 입력합니다.

```
node s3_setbucketwebsite.js BUCKET_NAME INDEX_PAGE ERROR_PAGE
```

이 샘플 코드는 [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_setbucketwebsite.js)에서 찾을 수 있습니다.

## 버킷 웹 사이트 구성 삭제
<a name="s3-example-static-web-host-delete-website"></a>

파일 이름이 `s3_deletebucketwebsite.js`인 Node.js 모듈을 생성합니다. 위와 같이 SDK를 구성해야 합니다. `AWS.S3` 서비스 객체를 생성합니다.

선택한 버킷에 대한 웹 사이트 구성을 삭제하는 함수를 생성합니다. `deleteBucketWebsite` 메서드를 호출할 때 전달해야 하는 유일한 파라미터는 선택한 버킷의 이름입니다.

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create S3 service object
s3 = new AWS.S3({ apiVersion: "2006-03-01" });

var bucketParams = { Bucket: process.argv[2] };

// call S3 to delete website configuration for selected bucket
s3.deleteBucketWebsite(bucketParams, function (error, data) {
  if (error) {
    console.log("Error", err);
  } else if (data) {
    console.log("Success", data);
  }
});
```

예제를 실행하려면 명령줄에서 다음을 입력합니다.

```
node s3_deletebucketwebsite.js BUCKET_NAME
```

이 샘플 코드는 [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/s3/s3_deletebucketwebsite.js)에서 찾을 수 있습니다.