macOS에서 aws cli 설치 및 명령어 예시
macOS에서 aws s3 cli를 설치하는 방법 및 명령어 예시
1. aws cli 설치
아래 두 명령어를 순차적으로 입력하면 설치가 완료된다.
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg ./AWSCLIV2.pkg -target /
설치 완료 후, 버전을 확인하여 정상적으로 설치된 것을 확인할 수 있다.
aws --version
2. aws configure 설정
먼저 aws console로 접속 후, 오른쪽 상단 아이디 클릭 후, 보안자격증명을 클릭한다.
중간에 엑세스 키 만들기를 클릭 후, Access Key, Secret Key를 복사한다.
terminal을 키고, 아래 명령어를 입력한다.
aws configure
4가지 항목을 입력하는데, 방금 복사한 Access Key, Secret Key를 입력하고, region과 포맷도 입력한다.
AWS Access Key ID [None] : Accecss Key
AWS Secret Access Key [None] : Secret Key
Default region name [None] : ap-northeast-2
Default output format [None] : json
설정값이 잘 들어갔는지 확인할 수 있다.
aws configure list
3. aws s3 명령어
버킷 또는 디렉토리의 용량 및 정보를 재귀적으로 확인
aws s3 ls s3://버킷이름/디렉토리 --summarize --human-readable --recursive
파일이름이 ab로 시작하고 4글자의 파일명을 가진 txt 파일을 s3로 복사
aws s3 cp ab??.txt s3://버킷이름/디렉토리
버킷에서 폴더 생성
aws s3api put-object --bucket 버킷이름 --key 폴더이름/
모든 txt파일을 싱크, 스토리지 클래스는 STANDARD_IA로 설정
aws s3 sync . s3://버킷이름/ --exclude='*' --include='*.txt' --storage-class=STANDARD_IA
Glacier 버킷에 있는 모든 파일에 대해 벌크 티어로 6일간 다운 받을 수 있도록 복구 요청
aws s3 ls s3://버킷이름/ --recursive | awk '{print $4}' | xargs -L 1 aws s3api restore-object --restore-request '{"Days":6,"GlacierJobParameters":{"Tier":"Bulk"}}' --bucket 버킷이름 --key
모든 오브젝트의 메타데이터 출력
aws s3 ls s3://버킷이름/ --recursive | awk '{print $4}' | xargs -L 1 aws s3api head-object --bucket 버킷이름 --key
멀티파트 업로드 파일 목록 조회 및 삭제
네트워크 에러 및 강제 중단의 이유로 업로드에 실패한 대용량 파일의 경우 aws console에서 보이지 않지만 파일이 존재하게 된다.
멀티파트 오브젝트 조회
aws s3api list-multipart-uploads --bucket 버킷이름
멀티파트 오브젝트 삭제
brew install jq
BUCKETNAME=버킷이름입력
aws s3api list-multipart-uploads --bucket $BUCKETNAME \
| jq -r '.Uploads[] | "--key \"\(.Key)\" --upload-id \(.UploadId)"' \
| while read -r line; do
eval "aws s3api abort-multipart-upload --bucket $BUCKETNAME $line";
done
참고자료
- https://docs.aws.amazon.com/ko_kr/cli/latest/userguide/getting-started-install.html
- https://stackoverflow.com/questions/39457458/howto-abort-all-incomplete-multipart-uploads-for-a-bucket
- https://docs.aws.amazon.com/AmazonS3/latest/API/API_AbortMultipartUpload.html
- https://mosei.tistory.com/entry/AWS-AWS-CLI%EB%A5%BC-%ED%86%B5%ED%95%B4-S3-%EB%A1%9C-%ED%8C%8C%EC%9D%BC-%EC%97%85%EB%A1%9C%EB%93%9C-%ED%95%98%EA%B8%B0
- https://docs.aws.amazon.com/cli/latest/reference/s3api/list-multipart-uploads.html
- https://stackoverflow.com/questions/56894349/aws-s3-sync-only-copy-files-with-a-certain-extension
- https://stackoverflow.com/questions/36837975/how-to-create-folder-on-s3-from-ec2-instance
- https://stackoverflow.com/questions/51670231/how-do-i-restore-from-aws-glacier-back-to-s3-permanently