-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from Louie-03/BE-feature-42
[BE] 파일 업로드 기능 구현
- Loading branch information
Showing
8 changed files
with
98 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
BE/src/main/java/louie/hanse/issuetracker/domain/UploadFile.java
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
BE/src/main/java/louie/hanse/issuetracker/service/S3Service.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
BE/src/main/java/louie/hanse/issuetracker/web/controller/FileUploadController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters