Skip to content

Commit

Permalink
[#82]: feat: 이미지 업로드 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
102092 committed Jun 22, 2020
1 parent ef4f47d commit af3ae90
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import com.codesquad.issue.domain.issue.Issue;
import com.codesquad.issue.domain.issue.IssueLabel;
import com.codesquad.issue.domain.issue.IssueLabelRepository;
import com.codesquad.issue.domain.issue.IssueRepository;
import com.codesquad.issue.domain.issue.request.IssueCreateRequest;
import com.codesquad.issue.domain.issue.request.IssueModifyRequest;
import com.codesquad.issue.domain.issue.response.IssueCreateResponse;
import com.codesquad.issue.domain.issue.response.IssueDetailResponse;
import com.codesquad.issue.domain.issue.IssueRepository;
import com.codesquad.issue.domain.issue.response.IssueResponse;
import com.codesquad.issue.domain.issue.request.IssueCreateRequest;
import com.codesquad.issue.domain.label.Label;
import com.codesquad.issue.domain.label.LabelRepository;
import com.codesquad.issue.domain.label.response.LabelResponse;
Expand Down
42 changes: 42 additions & 0 deletions BE/src/main/java/com/codesquad/issue/service/S3Service.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.codesquad.issue.service;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;
import java.io.File;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Slf4j
@Service
public class S3Service {

private final AmazonS3Client amazonS3Client;

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

public String upload(File uploadFile, String dirName) {
String fileName = dirName + "/" + uploadFile.getName();
String uploadImageUrl = putS3(uploadFile, fileName);
removeNewFile(uploadFile);
return uploadImageUrl;
}

private String putS3(File uploadFile, String fileName) {
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, uploadFile).withCannedAcl(
CannedAccessControlList.PublicRead));
return amazonS3Client.getUrl(bucket, fileName).toString();
}

private void removeNewFile(File targetFile) {
if (targetFile.delete()) {
log.info("파일이 삭제되었습니다.");
} else {
log.info("파일이 삭제되지 못했습니다.");
}
}
}

0 comments on commit af3ae90

Please sign in to comment.