feat: Google Drive API 인증 및 에러 처리 개선

This commit is contained in:
2025-08-28 11:58:36 +09:00
parent 59d213ab4a
commit f0ddc5aebe
4 changed files with 66 additions and 15 deletions

View File

@@ -22,10 +22,18 @@ class GoogleDriveUploader:
"""
Google Drive API 인증
"""
# 자격증명 파일 존재 여부 확인 (명시적으로 친절한 메시지 제공)
if not self.creds_path or not os.path.isfile(self.creds_path):
raise FileNotFoundError(
f"Google Drive API 자격증명 파일을 찾을 수 없습니다: {self.creds_path}.\n"
f"config.json의 'google_credentials_path'를 올바른 credentials.json 경로로 설정하거나, API 업로드를 사용하지 마세요."
)
token_path = os.path.join(os.path.dirname(self.creds_path) or '.', 'token.json')
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', self.scopes)
if os.path.exists(token_path):
creds = Credentials.from_authorized_user_file(token_path, self.scopes)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
@@ -35,7 +43,7 @@ class GoogleDriveUploader:
self.creds_path, self.scopes)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
with open(token_path, 'w') as token:
token.write(creds.to_json())
self.service = build('drive', 'v3', credentials=creds)
@@ -44,6 +52,9 @@ class GoogleDriveUploader:
"""
파일을 Google Drive에 업로드
"""
if self.service is None:
raise RuntimeError('Google Drive API가 초기화되지 않았습니다. credentials.json과 folder_id를 설정하세요.')
if file_name is None:
file_name = os.path.basename(file_path)
@@ -87,6 +98,9 @@ class GoogleDriveUploader:
"""
폴더 내 파일 목록 조회
"""
if self.service is None:
raise RuntimeError('Google Drive API가 초기화되지 않았습니다. credentials.json과 folder_id를 설정하세요.')
try:
results = self.service.files().list(
q=f"'{self.folder_id}' in parents",