diff --git a/app/Services/GoogleCloudStorageService.php b/app/Services/GoogleCloudStorageService.php index c00e30b2..e65e5357 100644 --- a/app/Services/GoogleCloudStorageService.php +++ b/app/Services/GoogleCloudStorageService.php @@ -169,7 +169,7 @@ public function upload(string $filePath, string $objectName): ?string /** * GCS에서 서명된 다운로드 URL 생성 * - * @param string $objectName GCS 객체 이름 + * @param string $objectName GCS 객체 이름 또는 gs:// URI * @param int $expiresInMinutes URL 유효 시간 (분) * @return string|null 서명된 URL 또는 실패 시 null */ @@ -179,6 +179,8 @@ public function getSignedUrl(string $objectName, int $expiresInMinutes = 60): ?s return null; } + $objectName = $this->stripGsPrefix($objectName); + $expiration = time() + ($expiresInMinutes * 60); $stringToSign = "GET\n\n\n{$expiration}\n/{$this->bucketName}/{$objectName}"; @@ -205,7 +207,7 @@ public function getSignedUrl(string $objectName, int $expiresInMinutes = 60): ?s /** * GCS에서 파일 삭제 * - * @param string $objectName GCS 객체 이름 + * @param string $objectName GCS 객체 이름 또는 gs:// URI * @return bool 성공 여부 */ public function delete(string $objectName): bool @@ -214,6 +216,8 @@ public function delete(string $objectName): bool return false; } + $objectName = $this->stripGsPrefix($objectName); + $accessToken = $this->getAccessToken(); if (!$accessToken) { return false; @@ -297,6 +301,26 @@ private function base64UrlEncode(string $data): string return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } + /** + * gs://bucket-name/objectName → objectName 변환 + * + * upload()가 gs:// URI를 반환하므로, getSignedUrl/delete에서 자동 처리 + */ + private function stripGsPrefix(string $objectName): string + { + $prefix = 'gs://' . $this->bucketName . '/'; + if (str_starts_with($objectName, $prefix)) { + return substr($objectName, strlen($prefix)); + } + + // gs://다른-버킷/ 형태도 처리 + if (str_starts_with($objectName, 'gs://')) { + $objectName = preg_replace('#^gs://[^/]+/#', '', $objectName); + } + + return $objectName; + } + /** * 버킷 이름 반환 */