

 [AWS SDK for JavaScript V3 API 참조 안내서](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/)는 AWS SDK for JavaScript 버전 3(V3)의 모든 API 작업을 자세히 설명합니다.

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

# JavaScript ES6/CommonJS 구문
<a name="sdk-example-javascript-syntax"></a>

AWS SDK for JavaScript 코드 예는 ECMAScript 6(ES6)로 작성되었습니다. ES6는 코드를 더 현대적이고 읽기 쉽게 만들고 더 많은 작업을 수행할 수 있도록 새로운 구문과 새로운 기능을 제공합니다.

ES6에서는 Node.js 버전 13.x 이상을 사용해야 합니다. 최신 버전의 Node.js를 다운로드하여 설치하려면 [Node.js downloads](https://nodejs.org/en/download)를 참조하세요. 그러나 원하는 경우 다음 지침을 사용하여 코드 예를 CommonJS 구문으로 변환할 수 있습니다.
+ 프로젝트 환경의 `package.json`에서 `"type" : "module"`을 제거합니다.
+ 모든 ES6 `import` 문을 CommonJS `require` 문으로 변환합니다. 예를 들어 다음과 같이 변환합니다.

  ```
  import { CreateBucketCommand } from "@aws-sdk/client-s3";
  import { s3 } from "./libs/s3Client.js";
  ```

  위 구문을 동등한 다음 CommonJS 문으로 변환합니다.

  ```
  const { CreateBucketCommand } = require("@aws-sdk/client-s3");
  const { s3 } = require("./libs/s3Client.js");
  ```
+ 모든 ES6 `export` 문을 CommonJS `module.exports` 문으로 변환합니다. 예를 들어 다음과 같이 변환합니다.

  ```
  export {s3}
  ```

  위 구문을 동등한 다음 CommonJS 문으로 변환합니다.

  ```
  module.exports = {s3}
  ```

다음 예에서는 ES6와 CommonJS 모두에서 Amazon S3 버킷을 생성하는 코드 예를 보여줍니다.

------
#### [ ES6 ]

libs/s3Client.js

```
// Create service client module using ES6 syntax.
import { S3Client } from "@aws-sdk/client-s3";
// Set the AWS region
const REGION = "eu-west-1"; //e.g. "us-east-1"
// Create Amazon S3 service object.
const s3 = new S3Client({ region: REGION });
// Export 's3' constant.
export {s3};
```

s3\$1createbucket.js

```
// Get service clients module and commands using ES6 syntax.
 import { CreateBucketCommand } from "@aws-sdk/client-s3";
 import { s3 } from "./libs/s3Client.js";

// Get service clients module and commands using CommonJS syntax.
// const { CreateBucketCommand } = require("@aws-sdk/client-s3");
// const { s3 } = require("./libs/s3Client.js");

// Set the bucket parameters
const bucketParams = { Bucket: "BUCKET_NAME" };

// Create the Amazon S3 bucket.
const run = async () => {
  try {
    const data = await s3.send(new CreateBucketCommand(bucketParams));
    console.log("Success", data.Location);
    return data;
  } catch (err) {
    console.log("Error", err);
  }
};
run();
```

------
#### [ CommonJS ]

libs/s3Client.js

```
// Create service client module using CommonJS syntax.
 const { S3Client } = require("@aws-sdk/client-s3");
// Set the AWS Region.
const REGION = "REGION"; //e.g. "us-east-1"
 // Create Amazon S3 service object.
const s3 = new S3Client({ region: REGION });
// Export 's3' constant.
 module.exports ={s3};
```

s3\$1createbucket.js

```
// Get service clients module and commands using CommonJS syntax.
const { CreateBucketCommand } = require("@aws-sdk/client-s3");
const { s3 } = require("./libs/s3Client.js");

// Set the bucket parameters
const bucketParams = { Bucket: "BUCKET_NAME" };

// Create the Amazon S3 bucket.
const run = async () => {
  try {
    const data = await s3.send(new CreateBucketCommand(bucketParams));
    console.log("Success", data.Location);
    return data;
  } catch (err) {
    console.log("Error", err);
  }
};
run();
```

------