-
[ AWS ] S3 용량과 객체수 모니터링 (s3api와 python)AWS Infra 2021. 11. 21. 23:32
s3api 명령어를 이용하여 특정 버킷의 용량과 객체 수를 추출
Input :
aws s3api list-objects --bucket '버킷명' --output json --query '[sum(Contents[].Size), length(Contents[])]'
Output :
[ 11, 9 ]
Python을 이용하여
InPut :
import subprocess import re from datetime import datetime bucketname = '버킷명' command = "aws s3api list-objects --bucket '%s' --output json --query '[sum(Contents[].Size), length(Contents[])]'" %bucketname # bytes 객체를 String으로 변경 result = subprocess.check_output(command, shell=True).decode() # 시계열을 사용하기 위해 logtime을 추가하여 딕셔너리로 형태로 저장 result = { str(datetime.today()) : [bucketname] + [ int(x) for x in re.findall(r'\d+', result)] } print(result)
Output :
{'2021-11-21 23:28:17.165307': ['버킷명', 23255, 30]}
S3권한은 ReadOnlyAccess 정도만.
https://docs.aws.amazon.com/cli/latest/reference/s3api/list-objects.html
list-objects — AWS CLI 1.22.10 Command Reference
Note: You are viewing the documentation for an older major version of the AWS CLI (version 1). AWS CLI version 2, the latest major version of AWS CLI, is now stable and recommended for general use. To view this page for the AWS CLI version 2, click here. F
docs.aws.amazon.com
'AWS Infra' 카테고리의 다른 글
[ AWS ] EC2 Linux root 로그인 허용 (0) 2022.02.02 [ AWS ] S3 Bucket policy (0) 2021.12.03 [ AWS ] IAM Policy 특정IP에서 접근시에만 권한 허용 (0) 2021.11.30 [ Python ] paramiko를 이용한 pem-key SSH 접속 (자동화 작업) (0) 2021.11.30 AWS Linux UTC 시간 한국시간으로 변경 (0) 2021.11.13 [ AWS CLI ] S3 명령어 (0) 2021.11.04 [ AWS ] IAM Policy 특정 S3만 읽기 / 쓰기 권한 부여 (0) 2021.11.04 [ AWS ] Linux AWS S3 설치 (0) 2021.11.01