Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

서버에서 이미지 업로드 기능을 추가한다. #42

Merged
merged 6 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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