GCS

Google Cloud Storage는 GCP에서 제공하는 객체 스토리지 서비스로, 파일·이미지·백업 등을 무제한으로 저장하고 URL로 접근할 수 있습니다.

핵심 특징

S3 vs GCS 비교

항목AWS S3GCS
제공사AWSGCP
버킷 위치리전 단위리전 / 멀티리전 / 듀얼리전
스토리지 클래스Standard / IA / GlacierStandard / Nearline / Coldline / Archive
API 호환—S3 호환 API 지원

스토리지 클래스

클래스접근 빈도최소 보관용도
Standard자주없음이미지, 정적 파일
Nearline월 1회 이하30일백업
Coldline분기 1회 이하90일재해 복구
Archive연 1회 이하365일장기 아카이브

Spring Boot 연동

implementation 'com.google.cloud:spring-cloud-gcp-starter-storage'
# application.yml
spring:
  cloud:
    gcp:
      project-id: cslog-rtaeho
      credentials:
        location: classpath:gcp-key.json
@Service
@RequiredArgsConstructor
public class GcsService {

    private final Storage storage;

    // 파일 업로드
    public String upload(String bucketName, String fileName, byte[] content, String contentType) {
        BlobId blobId = BlobId.of(bucketName, fileName);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
                .setContentType(contentType)
                .build();

        storage.create(blobInfo, content);
        return String.format("https://storage.googleapis.com/%s/%s", bucketName, fileName);
    }

    // 파일 다운로드
    public byte[] download(String bucketName, String fileName) {
        return storage.readAllBytes(bucketName, fileName);
    }

    // 파일 삭제
    public void delete(String bucketName, String fileName) {
        storage.delete(BlobId.of(bucketName, fileName));
    }

    // Signed URL 생성 (비공개 파일을 임시로 공개)
    public URL generateSignedUrl(String bucketName, String fileName) {
        BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, fileName).build();
        return storage.signUrl(blobInfo, 15, TimeUnit.MINUTES,
                Storage.SignUrlOption.withV4Signature());
    }
}

주요 사용 패턴

[이미지 업로드 흐름]
클라이언트 ──→ Spring Boot ──→ GCS 업로드
                    │
                    └──→ DB에 GCS URL 저장
                         (https://storage.googleapis.com/bucket/image.jpg)

[Signed URL — 비공개 파일 임시 접근]
클라이언트 ──→ Spring Boot ──→ Signed URL 발급 (유효기간 15분)
클라이언트 ──→ GCS 직접 접근 (Signed URL로)

언제 쓰나?

CSLOG에서 보기