📌 AWS SDK에서 Credential 및 Config 설정방법 (Windows 기준)
AWS SDK v3를 사용하는 경우 인증정보(credentials) 및 설정(config)을 별도 파일에서 관리할 수 있습니다.
✅ 1단계: Windows에서 파일 생성하기
다음 두 파일을 직접 생성합니다.
① credentials 파일 (AWS IAM 사용자 인증정보 설정)
- 경로:
%USERPROFILE%\.aws\credentials
- 내용 예시:
[default]
aws_access_key_id=AKIA***************
aws_secret_access_key=***************************
- 메모장에서 파일을 작성한 후, 이름을 credentials로 확장자 없이 저장합니다.
<br>
② config 파일 (리전 등 환경 설정)
- 경로:
%USERPROFILE%\.aws\config
- 내용 예시:
[default]
region=ap-northeast-2
- 마찬가지로 메모장에서 파일을 작성한 후, 이름을 config로 확장자 없이 저장합니다.
📂 파일 확인 예시
파일 구조는 다음과 같습니다:
C:\Users\사용자이름\.aws\
├── config
└── credentials
예를 들어 사용자 이름이 LG라면 경로는:
C:\Users\LG\.aws\credentials
C:\Users\LG\.aws\config
이렇게 됩니다.
✅ 2단계: Node.js 코드에서 자동으로 설정파일 사용하기
AWS SDK v3에서는 별도로 credentials를 코드에 하드코딩하지 않아도 자동으로 설정 파일을 불러옵니다.
다음과 같이 간단하게 사용 가능합니다:
import { S3Client, CreateBucketCommand, PutObjectCommand } from '@aws-sdk/client-s3';
// credentials와 config 파일에서 자동으로 읽음
const s3Client = new S3Client({});
async function main() {
const bucketName = 'rainsos--bucket-20250410';
await s3Client.send(new CreateBucketCommand({ Bucket: bucketName }));
console.log('✅ 버킷 생성 완료:', bucketName);
await s3Client.send(new PutObjectCommand({
Bucket: bucketName,
Key: 'hello.txt',
Body: 'Hello, JavaScript SDK!',
}));
console.log('✅ 파일 업로드 완료:', 'hello.txt');
}
main().catch(console.error);
💡 요약 (추천 방식)
항목파일위치파일내용
인증 정보 | %USERPROFILE%\.aws\credentials | AWS Access Key, Secret Key |
환경 설정 (리전 등) | %USERPROFILE%\.aws\config | AWS 리전 정보 |
✅ SDK 코드에 인증 정보를 하드코딩하는 것보다, 위의 파일 설정 방식을 권장합니다.
보안적으로도 더 안전하고 관리가 쉽습니다.
https://docs.aws.amazon.com/ko_kr/sdkref/latest/guide/file-location.html
AWS SDKs 및 도구의 공유 config 및 credentials 파일의 위치 찾기 및 변경
운영 체제 기본 위치 및 파일 이름
Linux 및 macOS
~/.aws/config
~/.aws/credentials
Windows
%USERPROFILE%\.aws\config
%USERPROFILE%\.aws\credentials
수동으로 해결 ( index.js에서 export 선언할때코드 삽입
const s3Client = new S3Client({
region: "ap-northeast-2",
credentials: {
accessKeyId: "YOUR_ACCESS_KEY_ID",
secretAccessKey: "YOUR_SECRET_ACCESS_KEY"
}
});
import {
S3Client,
CreateBucketCommand,
PutObjectCommand,
} from '@aws-sdk/client-s3';
export async function main() {
const s3Client = new S3Client({});
// S3 버킷 생성
const bucketName = 'soaple-bucket-20250410';
await s3Client.send(
new CreateBucketCommand({
Bucket: bucketName,
})
);
// S3 버킷에 파일 업로드
await s3Client.send(
new PutObjectCommand({
Bucket: bucketName,
Key: 'hello.txt',
Body: 'Hello, JavaScript SDK!',
})
);
}
main();
'AWS_Cloud' 카테고리의 다른 글
15강 SDK 자격증명 프로세스 windows AWS Cloud (0) | 2025.04.10 |
---|---|
13강 DynamoDB 및 Lamda 실습 노트 (AWS Cloud) (0) | 2025.04.10 |
13강 DynamoDB 테이블설계과정(Study) AWS Cloud (0) | 2025.04.10 |
13강 DynamoDB 강의 노트 AWS Cloud (0) | 2025.04.09 |
AWS 기본 서브넷 생성 (0) | 2025.04.09 |