💻 CSP/AWS

[S3] AWS CLI를 이용하여 S3 다루기

heywantodo 2023. 4. 11. 10:07
728x90
반응형

[S3] AWS CLI를 이용하여 S3 다루기

AWS CLI를 이용해서 서버에서 버킷에 파일을 업로드, 삭제할 수 있고

버킷 간 객체를 복사하거나 sync를 맞출 수 있음

 

AWS CLI 설치

아래 포스팅 참고하여 설치

https://heywantodo.tistory.com/49

 

[AWS] AWS CLI 설치 & 인증

[AWS] AWS CLI 설치 & 인증 AWS Command Line Interface는 쉘 커맨드를 사용하여 AWS 서비스와 상호 작용할 수 있는 도구 AWS CLI를 사용하여 서비스의 기능을 살펴보고 리소스를 관리할 스크립트를 개발할 수

heywantodo.tistory.com

 

S3 버킷 확인

  • 버킷 리스트
aws s3 ls
  • 버킷 내 객체 리스트
aws s3 ls s3://bucketname/

 

S3 객체 업로드

  • 로컬에 있는 파일 업로드
aws s3 cp filename s3://bucketname/filename
  • 파일을 모두가 읽을 수 있도록 권한 설정
aws s3 cp filename s3://bucketname/filename --acl public-read

 

S3 객체 삭제

  • 버킷에 있는 파일 삭제
aws s3 rm s3://bucketname/filename

 

S3 버킷 간 파일 동기화

  • 두 버킷의 콘텐츠를 동기화
  • 원본과 대상 간 누락되거나 오래된 파일 또는 객체를 복사
aws s3 sync s3://source-bucket/path s3://destination-bucket/path
  • 원본에 없는 파일이나 객체를 대상에서 제거할 수도 있음
aws s3 sync s3://source-bucket/path s3://destination-bucket/path --delete
  • 작업 중 삭제할 파일 또는 객체를 필터링 할 수 있음

✔ 제외 옵션 (--exclude)

: source 버킷에 없는 데이터는 Myfile?.txt를 제외하고 다 삭제

aws s3 sync s3://source-bucket/path s3://destination-bucket/path --delete --exclude "path/MyFile?.txt"

✔ 포함 옵션 (--include)

: source 버킷에 없는 데이터는 Myfile?.txt를 제외하고 다 삭제 하는데, Myfile517.txt는 포함

aws s3 sync s3://source-bucket/path s3://destination-bucket/path --delete --exclude "path/MyFile517.txt" --include "path/MyFile?.txt"

 

 

참조

https://docs.aws.amazon.com/ko_kr/cli/latest/userguide/cli-services-s3-commands.html#using-s3-commands-managing-objects-copy

728x90
반응형