boto3 설치 및 기본설정은 여기서 👉 [Python] boto3로 AWS S3와 연동하기
Amazon boto3 공식 문서를 참고하였음 👉 AWS boto3 document - S3
import boto3
AWS_ACCESS_KEY_ID ="[access_key]"
AWS_SECRET_ACCESS_KEY = "[secret_key]"
AWS_DEFAULT_REGION = "ap-northeast-2"
client = boto3.client('s3',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_DEFAULT_REGION
)
response = client.list_buckets() # bucket 목록
print(response)
import boto3
client = boto3.client('s3')
response = client.list_buckets() # bucket 목록
print(response)
import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all(): # bucket 목록
print(bucket.name)
import boto3
session = boto3.Session(
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_DEFAULT_REGION
)
s3 = session.resource('s3') # s3에 대한 권한 및 상태를 s3(변수)에 저장
for bucket in s3.buckets.all():
print(bucket.name)
BUCKET_NAME = "test"
buckets = s3.Bucket(name=BUCKET_NAME)
print(buckets)
# 출력: s3.Bucket(name='test')
for obj in buckets.objects.all():
print(obj)
print(obj.key)
# error 발생: An error occurred (AccessDenied)
# when calling the ListObjects operation: Access Denied
BUCKET_NAME = "test"
object_key = "testImg/test.png"
s3 = boto3.resource('s3')
object_ = s3.Object(BUCKET_NAME, object_key)
print(object_)
# 출력: s3.Object(bucket_name='test', key='testImg/test.png')