Skip to content

Commit

Permalink
서버에서 이미지 업로드 기능을 추가한다. (#42)
Browse files Browse the repository at this point in the history
* feat: spring cloud aws, aws-s3 의존성 추가

* feat: storage client 인터페이스 작성

* feat: aws cloud, s3 설정 추가

* feat: AwsS3Client upload, delete 메서드 구현

* feat: 테스트 aws s3 설정 추가

* chore: 빈 라인 정렬
  • Loading branch information
alstn113 authored Jan 31, 2025
1 parent 58428b5 commit b144c6e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
4 changes: 4 additions & 0 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ dependencies {
// monitoring
implementation 'org.springframework.boot:spring-boot-starter-actuator'

// aws
implementation platform('io.awspring.cloud:spring-cloud-aws-dependencies:3.1.1')
implementation 'io.awspring.cloud:spring-cloud-aws-starter-s3'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fluffy.storage.application;

import org.springframework.web.multipart.MultipartFile;

public interface StorageClient {

String upload(MultipartFile file);

void delete(String fileName);
}
67 changes: 67 additions & 0 deletions server/src/main/java/com/fluffy/storage/infra/AwsS3Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.fluffy.storage.infra;

import com.fluffy.global.exception.BadRequestException;
import com.fluffy.global.exception.NotFoundException;
import com.fluffy.storage.application.StorageClient;
import io.awspring.cloud.s3.S3Exception;
import io.awspring.cloud.s3.S3Resource;
import io.awspring.cloud.s3.S3Template;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
@RequiredArgsConstructor
public class AwsS3Client implements StorageClient {

private final S3Template s3Template;

@Value("${spring.cloud.aws.s3.bucket}")
private String bucketName;

@Override
public String upload(MultipartFile file) {

if (file.isEmpty()) {
throw new BadRequestException("파일이 비어있습니다.");
}

String fileName = generateFileName(file.getOriginalFilename());

try (InputStream is = file.getInputStream()) {
S3Resource upload = s3Template.upload(bucketName, fileName, is);

return upload.getURL().toString();
} catch (IOException | S3Exception e) {
throw new BadRequestException("파일 업로드에 실패했습니다.", e);
}
}

@Override
public void delete(String fileName) {
try {
s3Template.deleteObject(bucketName, fileName);
} catch (S3Exception e) {
throw new NotFoundException("파일을 찾을 수 없습니다.", e);
}
}

private String generateFileName(String originalFileName) {
if (originalFileName == null) {
throw new NotFoundException("파일 이름을 찾을 수 없습니다.");
}

int extensionIndex = originalFileName.lastIndexOf(".");

String extension = originalFileName.substring(extensionIndex);
String fileName = originalFileName.substring(0, extensionIndex);

String now = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS").format(System.currentTimeMillis());

return "%s-%s%s".formatted(fileName, now, extension);
}
}
19 changes: 19 additions & 0 deletions server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ spring:
redis:
host: localhost
port: 6379
cloud:
aws:
credentials:
access-key: ${AWS_ACCESS_KEY}
secret-key: ${AWS_SECRET_KEY}
region:
static: ${AWS_S3_REGION}
s3:
bucket: ${AWS_S3_BUCKET}


api-host: http://localhost:8080
client-host: http://localhost:5173
Expand Down Expand Up @@ -93,6 +103,15 @@ spring:
redis:
host: redis
port: 6379
cloud:
aws:
credentials:
access-key: ${AWS_ACCESS_KEY}
secret-key: ${AWS_SECRET_KEY}
region:
static: ${AWS_S3_REGION}
s3:
bucket: ${AWS_S3_BUCKET}

api-host: https://api.fluffy.run
client-host: https://www.fluffy.run
Expand Down
10 changes: 10 additions & 0 deletions server/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ spring:
redis:
host: localhost
port: 6379
cloud:
aws:
credentials:
access-key: access-key
secret-key: secret-key
region:
static: ap-northeast-2
s3:
bucket: bucket


api-host: http://localhost:8080
client-host: http://localhost:5173
Expand Down

0 comments on commit b144c6e

Please sign in to comment.