From 6b54e19a426fb098bdd34e6db27a3284526c829b Mon Sep 17 00:00:00 2001 From: pro Date: Thu, 29 Jan 2026 09:39:49 +0900 Subject: [PATCH] =?UTF-8?q?feat:GCS=20=ED=99=98=EA=B2=BD=EB=B3=80=EC=88=98?= =?UTF-8?q?(.env)=20=EA=B8=B0=EB=B0=98=20=EC=84=A4=EC=A0=95=20=EC=A7=80?= =?UTF-8?q?=EC=9B=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - config/gcs.php 추가: 환경변수 기반 설정 - GoogleCloudStorageService 우선순위 변경: 1. DB 설정 (UI 오버라이드용) 2. 환경변수 (.env) - 기본 3. 레거시 파일 (fallback) - .env.example에 GCS 설정 추가 - 서버 배포 시 .env만 설정하면 DB 설정 불필요 Co-Authored-By: Claude Opus 4.5 --- .env.example | 5 ++ app/Services/GoogleCloudStorageService.php | 59 +++++++++++++++++----- config/gcs.php | 21 ++++++++ 3 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 config/gcs.php diff --git a/.env.example b/.env.example index de90e952..4ad4d45b 100644 --- a/.env.example +++ b/.env.example @@ -81,3 +81,8 @@ FCM_LOG_CHANNEL=stack BAROBILL_CERT_KEY= BAROBILL_CORP_NUM= BAROBILL_TEST_MODE=true + +# Google Cloud Storage (음성 녹음 백업) +GCS_BUCKET_NAME= +GCS_SERVICE_ACCOUNT_PATH=/var/www/sales/apikey/google_service_account.json +GCS_USE_DB_CONFIG=true diff --git a/app/Services/GoogleCloudStorageService.php b/app/Services/GoogleCloudStorageService.php index 2b79e8cf..c00e30b2 100644 --- a/app/Services/GoogleCloudStorageService.php +++ b/app/Services/GoogleCloudStorageService.php @@ -8,13 +8,18 @@ /** * Google Cloud Storage 업로드 서비스 * - * DB 설정(ai_configs 테이블)을 우선 사용하고, 없으면 레거시 파일 설정을 사용합니다. + * 우선순위: + * 1. DB 설정 (ui에서 오버라이드) + * 2. 환경변수 (.env) + * 3. 레거시 파일 (/sales/apikey/) + * * JWT 인증 방식 사용. */ class GoogleCloudStorageService { private ?string $bucketName = null; private ?array $serviceAccount = null; + private string $configSource = 'none'; public function __construct() { @@ -26,30 +31,49 @@ public function __construct() * * 우선순위: * 1. DB 설정 (ai_configs 테이블의 활성화된 gcs provider) - * 2. 레거시 파일 설정 (/sales/apikey/) + * 2. 환경변수 (.env의 GCS_BUCKET_NAME, GCS_SERVICE_ACCOUNT_PATH) + * 3. 레거시 파일 설정 (/sales/apikey/) */ private function loadConfig(): void { - // 1. DB 설정 확인 - $dbConfig = AiConfig::getActiveGcs(); + // 1. DB 설정 확인 (GCS_USE_DB_CONFIG=true일 때만) + if (config('gcs.use_db_config', true)) { + $dbConfig = AiConfig::getActiveGcs(); - if ($dbConfig) { - $this->bucketName = $dbConfig->getBucketName(); + if ($dbConfig) { + $this->bucketName = $dbConfig->getBucketName(); - // 서비스 계정: JSON 직접 입력 또는 파일 경로 - if ($dbConfig->getServiceAccountJson()) { - $this->serviceAccount = $dbConfig->getServiceAccountJson(); - } elseif ($dbConfig->getServiceAccountPath() && file_exists($dbConfig->getServiceAccountPath())) { - $this->serviceAccount = json_decode(file_get_contents($dbConfig->getServiceAccountPath()), true); + // 서비스 계정: JSON 직접 입력 또는 파일 경로 + if ($dbConfig->getServiceAccountJson()) { + $this->serviceAccount = $dbConfig->getServiceAccountJson(); + } elseif ($dbConfig->getServiceAccountPath() && file_exists($dbConfig->getServiceAccountPath())) { + $this->serviceAccount = json_decode(file_get_contents($dbConfig->getServiceAccountPath()), true); + } + + if ($this->bucketName && $this->serviceAccount) { + $this->configSource = 'db'; + Log::debug('GCS 설정 로드: DB (활성화된 설정: ' . $dbConfig->name . ')'); + return; + } } + } + + // 2. 환경변수 (.env) 설정 + $envBucket = config('gcs.bucket_name'); + $envServiceAccountPath = config('gcs.service_account_path'); + + if ($envBucket && $envServiceAccountPath && file_exists($envServiceAccountPath)) { + $this->bucketName = $envBucket; + $this->serviceAccount = json_decode(file_get_contents($envServiceAccountPath), true); if ($this->serviceAccount) { - Log::debug('GCS 설정 로드: DB (활성화된 설정: ' . $dbConfig->name . ')'); + $this->configSource = 'env'; + Log::debug('GCS 설정 로드: 환경변수 (.env)'); return; } } - // 2. 레거시 파일 설정 (fallback) + // 3. 레거시 파일 설정 (fallback) $gcsConfigPath = base_path('../sales/apikey/gcs_config.txt'); if (file_exists($gcsConfigPath)) { $config = parse_ini_file($gcsConfigPath); @@ -62,10 +86,19 @@ private function loadConfig(): void } if ($this->bucketName && $this->serviceAccount) { + $this->configSource = 'legacy'; Log::debug('GCS 설정 로드: 레거시 파일'); } } + /** + * 현재 설정 소스 반환 + */ + public function getConfigSource(): string + { + return $this->configSource; + } + /** * GCS가 사용 가능한지 확인 */ diff --git a/config/gcs.php b/config/gcs.php new file mode 100644 index 00000000..44078e43 --- /dev/null +++ b/config/gcs.php @@ -0,0 +1,21 @@ + env('GCS_BUCKET_NAME'), + + // 서비스 계정 파일 경로 + 'service_account_path' => env('GCS_SERVICE_ACCOUNT_PATH', '/var/www/sales/apikey/google_service_account.json'), + + // DB 설정 사용 여부 (false면 .env만 사용) + 'use_db_config' => env('GCS_USE_DB_CONFIG', true), +];