-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupload_to_s3.py
More file actions
118 lines (100 loc) · 3.55 KB
/
upload_to_s3.py
File metadata and controls
118 lines (100 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
"""
S3에 이미지 파일을 업로드하는 스크립트
"""
import os
import sys
import boto3
from dotenv import load_dotenv
from botocore.exceptions import ClientError
# .env.dev 파일 로드
load_dotenv('.env.dev')
def upload_file_to_s3(local_file_path, s3_key):
"""
S3 버킷에 파일 업로드
Args:
local_file_path: 로컬 파일 경로
s3_key: S3에 저장될 키 (경로)
"""
# AWS 설정
aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
bucket_name = 'posting-files'
region = 'ap-northeast-2'
if not aws_access_key_id or not aws_secret_access_key:
print("Error: AWS credentials not found in environment variables")
return False
# S3 클라이언트 생성
s3_client = boto3.client(
's3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region
)
try:
# 파일 업로드 (ACL 없이, 버킷 정책으로 public 접근)
print(f"Uploading {local_file_path} to s3://{bucket_name}/{s3_key}")
s3_client.upload_file(
local_file_path,
bucket_name,
s3_key,
ExtraArgs={
'ContentType': 'image/jpeg'
}
)
print(f"✓ Successfully uploaded!")
print(f"URL: https://{bucket_name}.s3.{region}.amazonaws.com/{s3_key}")
return True
except FileNotFoundError:
print(f"Error: File not found: {local_file_path}")
return False
except ClientError as e:
print(f"Error uploading to S3: {e}")
return False
def list_s3_files(prefix='paper_review/card_imgs/'):
"""
S3 버킷의 파일 목록 조회
"""
aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
bucket_name = 'posting-files'
region = 'ap-northeast-2'
s3_client = boto3.client(
's3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region
)
try:
response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=prefix)
if 'Contents' in response:
print(f"\nFiles in s3://{bucket_name}/{prefix}:")
for obj in response['Contents']:
print(f" - {obj['Key']} ({obj['Size']} bytes)")
else:
print(f"No files found in s3://{bucket_name}/{prefix}")
except ClientError as e:
print(f"Error listing S3 files: {e}")
if __name__ == "__main__":
print("=== S3 Upload Script ===\n")
# 현재 S3 버킷 파일 목록 확인
print("Checking current files in S3...")
list_s3_files()
print("\n" + "="*50)
print("Upload examples:")
print(" python upload_to_s3.py upload /path/to/가을.jpg paper_review/card_imgs/autumn.jpg")
print(" python upload_to_s3.py upload /path/to/숲속집.jpg paper_review/card_imgs/forest_house.jpg")
print(" python upload_to_s3.py list")
print("="*50 + "\n")
# 커맨드 라인 인자 처리
if len(sys.argv) > 1:
command = sys.argv[1]
if command == "upload" and len(sys.argv) == 4:
local_path = sys.argv[2]
s3_key = sys.argv[3]
upload_file_to_s3(local_path, s3_key)
elif command == "list":
prefix = sys.argv[2] if len(sys.argv) > 2 else 'paper_review/card_imgs/'
list_s3_files(prefix)
else:
print("Invalid command or arguments")