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

#18 feat: S3 API 구현 #23

Merged
merged 1 commit into from
Aug 10, 2023
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
1 change: 1 addition & 0 deletions place/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand Down
32 changes: 32 additions & 0 deletions place/src/main/java/com/umc/place/common/config/AWSConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.umc.place.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

@Configuration
public class AWSConfig {

@Value("${cloud.aws.credentials.accessKey}")
private String accessKey;

@Value("${cloud.aws.credentials.secretKey}")
private String secretKey;

@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.umc.place.common.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.umc.place.common.service.S3Upload;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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;

@RequiredArgsConstructor
@RestController
public class FileUploadController {

private final S3Upload s3Upload;

@PostMapping("/upload")
public ResponseEntity<Object> uploadFile(MultipartFile[] multipartFileLis) throws IOException {
return ResponseEntity.ok(
s3Upload.upload(multipartFileLis)
);
}
}
79 changes: 79 additions & 0 deletions place/src/main/java/com/umc/place/common/service/S3Upload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.umc.place.common.service;

import com.amazonaws.services.s3.AmazonS3;
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 lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RequiredArgsConstructor
@Service
public class S3Upload {

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

private final AmazonS3Client amazonS3Client;
;

//https://jforj.tistory.com/261
public List<String> upload(MultipartFile[] multipartFileList) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저희는 이미지가 여러장이 아니라 그냥 String으로 반환해도 될 것 같아요!

List<String> imagePathList = new ArrayList<>();

for (MultipartFile multipartFile : multipartFileList) {
String originalName = multipartFile.getOriginalFilename(); // 파일 이름
long size = multipartFile.getSize(); // 파일 크기

ObjectMetadata objectMetaData = new ObjectMetadata();
objectMetaData.setContentType(multipartFile.getContentType());
objectMetaData.setContentLength(size);

// S3에 업로드
amazonS3Client.putObject(
new PutObjectRequest(bucket, originalName, multipartFile.getInputStream(), objectMetaData)
.withCannedAcl(CannedAccessControlList.PublicRead)
);

String imagePath = amazonS3Client.getUrl(bucket, originalName).toString(); // 접근가능한 URL 가져오기
imagePathList.add(imagePath);
}
return imagePathList;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DB에 이미지 url 저장하는 부분이 추가되어야 할 것 같긴 합니다!

// String s3FileName = UUID.randomUUID() + "-" + multipartFile.getOriginalFilename();
//
// ObjectMetadata objMeta = new ObjectMetadata();
// objMeta.setContentLength(multipartFile.getInputStream().available());
//
// amazonS3.putObject(bucket, s3FileName, multipartFile.getInputStream(), objMeta);
//
// return amazonS3.getUrl(bucket, s3FileName).toString();
// for(MultipartFile multipartFile: multipartFileList) {
// String originalName = multipartFile.getOriginalFilename(); // 파일 이름
// long size = multipartFile.getSize(); // 파일 크기
//
// ObjectMetadata objectMetaData = new ObjectMetadata();
// objectMetaData.setContentType(multipartFile.getContentType());
// objectMetaData.setContentLength(size);
//
// // S3에 업로드
// amazonS3Client.putObject(
// new PutObjectRequest(S3Bucket, originalName, multipartFile.getInputStream(), objectMetaData)
// .withCannedAcl(CannedAccessControlList.PublicRead)
// );
//
// String imagePath = amazonS3Client.getUrl(S3Bucket, originalName).toString(); // 접근가능한 URL 가져오기
// imagePathList.add(imagePath);
// }
//
// return new ResponseEntity<Object>(imagePathList, HttpStatus.OK);