

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

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

# Amazon SNS에서 메시지 게시
<a name="sns-examples-publishing-messages"></a>

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

**이 Node.js 코드 예제는 다음을 보여 줍니다.**
+ Amazon SNS 주제에 메시지를 게시하는 방법

## 시나리오
<a name="sns-examples-publishing-messages-scenario"></a>

이 예에서는 일련의 Node.js 모듈을 사용하여 Amazon SNS의 메시지를 주제 엔드포인트, 이메일 또는 전화번호에 게시합니다. 이 Node.js 모듈은 SDK for JavaScript에서 `SNS` 클라이언트 클래스의 다음 메서드를 사용하여 메시지를 전송합니다.
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-sns/Class/PublishCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-sns/Class/PublishCommand/)

## 사전 필수 작업
<a name="sns-examples-publishing-messages-prerequisites"></a>

이 예제를 설정하고 실행하려면 먼저 이러한 작업들을 완료해야 합니다.
+ 이러한 노드 TypeScript 예제를 실행하도록 프로젝트 환경을 설정하고 필요한 AWS SDK for JavaScript 모듈과 타사 모듈을 설치합니다. [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/sns/README.md)의 지침을 따릅니다.
+ 사용자 자격 증명을 사용하여 공유 구성 파일을 생성합니다. 공유 보안 인증 파일 제공에 관한 자세한 내용은 *AWS SDK 및 도구 참조 가이드*의 [Shared config and credentials files](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html) 단원을 참조하세요.

**중요**  
이 예는 ECMAScript6(ES6)를 사용하여 클라이언트 서비스 객체 및 명령을 가져오거나 내보내는 방법을 보여줍니다.  
따라서 Node.js 버전 13.x 이상이 필요합니다. 최신 버전의 Node.js를 다운로드하여 설치하려면 [Node.js downloads](https://nodejs.org/en/download)를 참조하세요.
CommonJS 구문을 사용하려는 경우 [JavaScript ES6/CommonJS 구문](sdk-example-javascript-syntax.md) 단원을 참조하세요.

## SNS 주제에 메시지 게시
<a name="sns-examples-publishing-text-messages"></a>

이 예에서는 Node.js 모듈을 사용하여 Amazon SNS 주제에 메시지를 게시합니다.

`libs` 디렉터리를 생성하고 파일 이름이 `snsClient.js`인 Node.js 모듈을 생성합니다. 이 모듈에 아래 코드를 복사하여 붙여 넣으면 Amazon SNS 클라이언트 객체가 생성됩니다. *REGION*을 해당 AWS 리전으로 바꿉니다.

```
import { SNSClient } from "@aws-sdk/client-sns";

// The AWS Region can be provided here using the `region` property. If you leave it blank
// the SDK will default to the region set in your AWS config.
export const snsClient = new SNSClient({});
```

이 코드 예는 [여기 GitHub에서](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/sns/libs/snsClient.js) 찾을 수 있습니다.

 파일 이름이 `publish-topic.js`인 Node.js 모듈을 생성합니다. 위와 같이 SDK를 구성합니다.

메시지 텍스트와 Amazon SNS 주제의 Amazon 리소스 이름(ARN)을 비롯하여 메시지 게시를 위한 파라미터가 포함된 객체를 생성합니다. 사용 가능한 SMS 속성에 대한 세부 정보는 [SetSMSAttributes](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#setSMSAttributes-property)를 참조하세요.

`SNS` 클라이언트 클래스의 `PublishCommand` 메서드에 파라미터를 전달합니다. Amazon SNS 클라이언트 서비스 객체를 호출하는 비동기 함수를 생성하여 파라미터 객체를 전달합니다.

**참고**  
*MESSAGE\$1TEXT*를 메시지 텍스트로 바꾸고, *TOPIC\$1ARN*을 SNS 주제의 ARN으로 바꿉니다.

```
import { PublishCommand } from "@aws-sdk/client-sns";
import { snsClient } from "../libs/snsClient.js";

/**
 * @param {string | Record<string, any>} message - The message to send. Can be a plain string or an object
 *                                                 if you are using the `json` `MessageStructure`.
 * @param {string} topicArn - The ARN of the topic to which you would like to publish.
 */
export const publish = async (
  message = "Hello from SNS!",
  topicArn = "TOPIC_ARN",
) => {
  const response = await snsClient.send(
    new PublishCommand({
      Message: message,
      TopicArn: topicArn,
    }),
  );
  console.log(response);
  // {
  //   '$metadata': {
  //     httpStatusCode: 200,
  //     requestId: 'e7f77526-e295-5325-9ee4-281a43ad1f05',
  //     extendedRequestId: undefined,
  //     cfId: undefined,
  //     attempts: 1,
  //     totalRetryDelay: 0
  //   },
  //   MessageId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
  // }
  return response;
};
```

예를 실행하려면 명령 프롬프트에서 다음을 입력합니다.

```
node publish-topic.js
```

이 코드 예는 [여기 GitHub에서](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/sns/actions/publish-topic.js) 찾을 수 있습니다.