๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/AWS(Amazon Web Services)

[AWS] Pyhton boto3 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ

by Jay Din 2024. 2. 16.
728x90
๋ฐ˜์‘ํ˜•

boto3 ๋ž€?

https://docs.aws.amazon.com/ko_kr/pythonsdk/?icmpid=docs_homepage_sdktoolkits

 

boto3๋Š” ํŒŒ์ด์ฌ์—์„œ Amazon Web Services(AWS)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ณต์‹ SDK(์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ ํ‚คํŠธ)์ž…๋‹ˆ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด AWS์˜ ๋‹ค์–‘ํ•œ ์„œ๋น„์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฃผ๋กœ ํด๋ผ์šฐ๋“œ ์„œ๋น„์Šค๋ฅผ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์œผ๋กœ ๋‹ค๋ฃจ๋Š” ๋ฐ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

boto3๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด EC2 ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ, S3 ๋ฒ„ํ‚ท ๊ด€๋ฆฌ, DynamoDB ํ…Œ์ด๋ธ” ์กฐ์ž‘, Lambda ํ•จ์ˆ˜ ๋ฐฐํฌ ๋“ฑ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

boto3 ์„ค์น˜ํ•˜๊ธฐ

๋จผ์ € boto3๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ pip๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

pip install boto3

 

์‚ฌ์šฉ๋ฒ• ์˜ˆ์‹œ

์•„๋ž˜ ์˜ˆ์‹œ๋Š” S3 ๋ฒ„ํ‚ท์„ ๋งŒ๋“ค๊ณ  ๋ชฉ๋ก์„ ๋‚˜์—ดํ•˜๋Š” ๊ฐ„๋‹จํ•œ boto3 ์‚ฌ์šฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

import boto3

# AWS ์ธ์ฆ ์„ค์ •
s3 = boto3.resource('s3', aws_access_key_id='YOUR_ACCESS_KEY_ID', aws_secret_access_key='YOUR_SECRET_ACCESS_KEY', region_name='YOUR_REGION')

# S3 ๋ฒ„ํ‚ท ๋งŒ๋“ค๊ธฐ
bucket_name = 'your-unique-bucket-name'
s3.create_bucket(Bucket=bucket_name)

# ์ƒ์„ฑํ•œ ๋ฒ„ํ‚ท ๋ชฉ๋ก ์ถœ๋ ฅ
for bucket in s3.buckets.all():
    print(bucket.name)

 

์‚ฌ์šฉ๋ฒ• ์„ค๋ช…

  1. `boto3.resource()` ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ AWS ๋ฆฌ์†Œ์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ `aws_access_key_id`, `aws_secret_access_key`, `region_name`์€ AWS ๊ณ„์ •์˜ ์ž๊ฒฉ ์ฆ๋ช… ๋ฐ ๋ฆฌ์ „์„ ์„ค์ •ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  2. `s3.create_bucket()` ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด S3 ๋ฒ„ํ‚ท์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  3. `s3.buckets.all()`์„ ํ†ตํ•ด ํ˜„์žฌ AWS ๊ณ„์ •์˜ ๋ชจ๋“  ๋ฒ„ํ‚ท ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

 

์ฐธ๊ณ  ์‚ฌํ•ญ

  • ์ด ์˜ˆ์‹œ๋Š” AWS ์ž๊ฒฉ ์ฆ๋ช…์„ ํ•˜๋“œ์ฝ”๋”ฉ์œผ๋กœ ์ œ๊ณตํ–ˆ์ง€๋งŒ, ์‹ค์ œ ํ™˜๊ฒฝ์—์„œ๋Š” ๋ณด์•ˆ์„ ์œ„ํ•ด IAM(Identity and Access Management) ์—ญํ• ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž๊ฒฉ ์ฆ๋ช…์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
  • ๋” ๋งŽ์€ AWS ์„œ๋น„์Šค์— ๋Œ€ํ•œ ์ •๋ณด์™€ ์‚ฌ์šฉ๋ฒ•์€ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•