Skip to content

Commit

Permalink
Merge pull request #50 from Louie-03/BE-feature-42
Browse files Browse the repository at this point in the history
[BE] 파일 업로드 기능 구현
  • Loading branch information
Louie-03 committed Jun 27, 2022
2 parents 3bee614 + 42a5cc0 commit 8c3470e
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 48 deletions.
1 change: 1 addition & 0 deletions BE/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.8.0'
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
implementation 'com.auth0:java-jwt:3.19.1'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
Expand Down
3 changes: 0 additions & 3 deletions BE/src/main/java/louie/hanse/issuetracker/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public class Comment {
@Id @GeneratedValue(strategy = IDENTITY)
private Long id;

@OneToMany(mappedBy = "comment")
private List<UploadFile> uploadFiles = new ArrayList<>();

@ManyToOne(fetch = LAZY)
@JoinColumn
private Issue issue;
Expand Down
21 changes: 0 additions & 21 deletions BE/src/main/java/louie/hanse/issuetracker/domain/UploadFile.java

This file was deleted.

47 changes: 47 additions & 0 deletions BE/src/main/java/louie/hanse/issuetracker/service/S3Service.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package louie.hanse.issuetracker.service;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RequiredArgsConstructor
@Service
public class S3Service {

private final AmazonS3Client amazonS3Client;

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

@Value("${directory.name}")
private String directoryName;

public String fileUpload(MultipartFile multipartFile) throws IOException {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(multipartFile.getContentType());
objectMetadata.setContentLength(multipartFile.getSize());

String originalFilename = multipartFile.getOriginalFilename();
int index = originalFilename.lastIndexOf(".");
String ext = originalFilename.substring(index);

String key = directoryName + "/" + UUID.randomUUID() + ext;
try (InputStream inputStream = multipartFile.getInputStream()) {
PutObjectRequest putObjectRequest = new PutObjectRequest(
bucket, key, inputStream, objectMetadata)
.withCannedAcl(CannedAccessControlList.PublicRead);
amazonS3Client.putObject(putObjectRequest);
}
return amazonS3Client.getUrl(bucket, key).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package louie.hanse.issuetracker.web.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import louie.hanse.issuetracker.service.S3Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RequiredArgsConstructor
@RequestMapping("/upload-files")
@RestController
public class FileUploadController {
private final S3Service s3Service;

@PostMapping
public Map<String, String> fileUpload(MultipartFile file) throws IOException {
return Collections.singletonMap("url", s3Service.fileUpload(file));
}
}
13 changes: 11 additions & 2 deletions BE/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ spring:
defer-datasource-initialization: true

config:
import: classpath:oauth.yml
import: classpath:oauth.yml,classpath:s3.yml

servlet:
multipart:
max-file-size: 10MB

logging.level:
org.hibernate.SQL: debug
org:
hibernate.SQL: debug
com:
amazonaws:
util:
EC2MetadataUtils: error
15 changes: 15 additions & 0 deletions BE/src/main/resources/s3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cloud:
aws:
region:
static: ap-northeast-2
s3:
bucket: louie1se-file-upload
credentials:
access-key: ${FILE_UPLOAD_S3_ACCESS_KEY}
secret-key: ${FILE_UPLOAD_S3_SECRET_KEY}
stack:
auto: false

directory:
name:
louie1se
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@
class IssueTrackerApplicationTests {

@Test
void contextLoads() throws Exception {
throwCheckedException();
}

void throwCheckedException() {
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}

void throwUnCheckedException() {
throw new RuntimeException();
}

static class CheckedException extends Exception {

}

static class UnCheckedException extends RuntimeException {

void contextLoads() {
}
}

0 comments on commit 8c3470e

Please sign in to comment.