아미(아름다운미소)

s3디버깅 본문

카테고리 없음

s3디버깅

유키공 2025. 4. 21. 17:16

IAM → 사용자 → 해당 사용자 클릭 → "권한" 탭 → 정책 추가

AmazonS3FullAccess 또는 최소 다음 권한 포함:

콘솔에서 권한 확인하는 방법

1. AWS 콘솔 접속: https://console.aws.amazon.com/
2. 좌측 상단 검색창에 IAM 입력 → 이동
3. 좌측 "사용자" 클릭
4. → 코드에서 조회된 사용자 이름 클릭
5. → "권한" 탭 확인
6. AmazonS3FullAccess 또는 아래 정책이 있어야 boto3로 S3 접근 가능:

{
  "Effect": "Allow",
  "Action": [
    "s3:ListBucket",
    "s3:GetObject",
    "s3:PutObject",
    "s3:DeleteObject"
  ],
  "Resource": [
    "arn:aws:s3:::your-bucket-name",
    "arn:aws:s3:::your-bucket-name/*"
  ]
}
import boto3
from botocore.exceptions import ClientError, EndpointConnectionError
from botocore.config import Config

# ============================
# AWS 인증 정보 입력
# ============================
AWS_ACCESS_KEY = '여기에_ACCESS_KEY_입력'
AWS_SECRET_KEY = '여기에_SECRET_KEY_입력'
REGION = 'ap-northeast-2'  # 버킷 리전 확인 후 맞게 설정

# ============================
# 공통 boto3 config
# ============================
config = Config(
    region_name=REGION,
    connect_timeout=5,
    read_timeout=10,
    retries={'max_attempts': 2}
)

# ============================
# 1. 현재 IAM 사용자 정보 출력
# ============================
try:
    sts = boto3.client(
        'sts',
        aws_access_key_id=AWS_ACCESS_KEY,
        aws_secret_access_key=AWS_SECRET_KEY,
        config=config
    )
    identity = sts.get_caller_identity()
    print("✅ 현재 IAM 사용자 정보:")
    print("Account:", identity['Account'])
    print("UserId:", identity['UserId'])
    print("ARN:", identity['Arn'])
except ClientError as e:
    print("❌ IAM 사용자 정보 조회 실패:", e)
except EndpointConnectionError as e:
    print("❌ 네트워크 연결 오류:", e)

# ============================
# 2. S3 클라이언트 연결
# ============================
s3 = boto3.client(
    's3',
    aws_access_key_id=AWS_ACCESS_KEY,
    aws_secret_access_key=AWS_SECRET_KEY,
    config=config
)

# ============================
# 3. 버킷 목록 조회
# ============================
try:
    response = s3.list_buckets()
    print("✅ 접근 가능한 S3 버킷 목록:")
    for bucket in response['Buckets']:
        print("-", bucket['Name'])
except ClientError as e:
    print("❌ S3 버킷 조회 실패:", e)

# ============================
# 4. 특정 버킷의 객체 목록 확인
# ============================
BUCKET_NAME = 'your-bucket-name'  # 실제 버킷 이름 입력

try:
    response = s3.list_objects_v2(Bucket=BUCKET_NAME)
    print(f"\n✅ 버킷 '{BUCKET_NAME}' 안의 파일 목록:")
    if 'Contents' in response:
        for obj in response['Contents']:
            print("-", obj['Key'])
    else:
        print("버킷이 비어 있습니다.")
except ClientError as e:
    print(f"❌ '{BUCKET_NAME}' 버킷 접근 실패:", e)
Comments