AWS Cloud9을 활용한 AWS SDK 활용(python을 활용한 s3생성)
2022. 8. 19. 09:54ㆍCloud/AWS 기초
먼저 아래의 명령어를 터미널에서 실행해 보자.
curl -O https://bootstrap.pypa.io/get-pip.py
python get-pip.py
==> python용 bootstrap 설치
python -m pip --version
==> 버전 확인
rm get-pip.py
==> 제거 명령어
python -m pip install boto3
==> boto3 설치 명령어
python -m pip show boto3
==> boto3 버전 확인
이 후 아래의 명령어를 입력한 후 실행해 보자. 단순 s3를 만들고 지우는 단순 작업 명령어이다.
import sys
import boto3
from botocore.exceptions import ClientError
def get_s3(region=None):
"""
Get a Boto 3 Amazon S3 resource with a specific AWS Region or with your
default AWS Region.
"""
return boto3.resource('s3', region_name=region) if region else boto3.resource('s3')
def list_my_buckets(s3):
print('Buckets:\n\t', *[b.name for b in s3.buckets.all()], sep="\n\t")
def create_and_delete_my_bucket(bucket_name, region, keep_bucket):
s3 = get_s3(region)
list_my_buckets(s3)
try:
print('\nCreating new bucket:', bucket_name)
bucket = s3.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={
'LocationConstraint': region
}
)
except ClientError as e:
print(e)
sys.exit('Exiting the script because bucket creation failed.')
bucket.wait_until_exists()
list_my_buckets(s3)
if not keep_bucket:
print('\nDeleting bucket:', bucket.name)
bucket.delete()
bucket.wait_until_not_exists()
list_my_buckets(s3)
else:
print('\nKeeping bucket:', bucket.name)
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('bucket_name', help='The name of the bucket to create.')
parser.add_argument('region', help='The region in which to create your bucket.')
parser.add_argument('--keep_bucket', help='Keeps the created bucket. When not '
'specified, the bucket is deleted '
'at the end of the demo.',
action='store_true')
args = parser.parse_args()
create_and_delete_my_bucket(args.bucket_name, args.region, args.keep_bucket)
if __name__ == '__main__':
main()
이 후 오른쪽 하단에 Command 에 s3.py(파일이름) s3.(도메인) 주소 --keep~(명령어) 를 입력한 후 실행하면 아래와 같은 화면이 나타날 것이다. 기존의 버켓과, 새로 생성한 버켓, 그리고 생성완료 후 어떤 버켓이 S3에 있는지 확인시켜준다.
'Cloud > AWS 기초' 카테고리의 다른 글
AWS EKS로 안정적인 서비스 운영하기 (0) | 2022.08.23 |
---|---|
AWS Cloud 9 (0) | 2022.08.19 |
AWS CodePipeIine 활용하기 part.1(빌드단계완성) (0) | 2022.08.15 |
AWS Resource관리(TAG/AWS 비용 관리 도구) (0) | 2022.08.05 |
AWS Sage Maker 활용part.2 (AI/ML기능 활용하기) (0) | 2022.08.04 |