버킷을 생성할 때 주의할 점은 이름에 대문자가 들어가면 안되고 특수문자가 들어가면안된다. 또한, 이 버킷의 이름은 AWS S3의 전체 버킷 이름들과 중복이 되면 안된다. 예를들어 test라는 버킷을 한 경우 누군가 test라는 버킷을 사용중이라면 test라는 이름으로 버킷을 생성할 수 없다.
이름을 정한 후 옵션이나 권한 설정은 필요하면 설정을 하시고 그 외 그냥 다음을 눌러 생성한다. 어차피 옵션이나 권한은 S3를 생성하고 다시 설정을 할 수 있다.
2. Spring boot pom.xml에 dependency를 추가한다.
s3 dependency를 추가한다.
com.amazonawsaws-java-sdk-s31.11.452
그리고 Multipartfile 을 사용할 것이기 때문에 Multipartfile dependency를 추가한다.
commons-fileuploadcommons-fileupload1.3.3
3. appliaction.properties 파일 설정.
리전 정보는 옆의 링크에서 확인할 수 있다.- https://docs.aws.amazon.com/ko_kr/general/latest/gr/rande.html
cloud.aws.credentials.accessKey=IAM USER에서 확인
cloud.aws.credentials.secretKey=IAM USER에서 확인
cloud.aws.s3.bucket=buckettestgogo
cloud.aws.region.static=ap-northeast-2
추가로 Mutlipartfile의 파일 크기 범위를 정하고 싶으면 application.properties에 아래와 같이 추가한다.
public class TestDTO { private Integer id; private String password; public TestDTO() { super(); } public TestDTO(Integer id, String password) { super(); this.id = id; this.password = password; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
다음 컨트롤러를 하나 생성해준다.
@RestController @RequestMapping(value = "/jsontest") public class TestClass { @GetMapping() public TestDTO test() { TestDTO test = new TestDTO(1, "1111"); return test; } @GetMapping("/2") public List test2() { List test = new ArrayList(); test.add(0, new TestDTO(1, "1111")); test.add(1, new TestDTO(2, "2222")); test.add(2, new TestDTO(3, "3333")); return test; } }
그리고 실행하게 되면
1. test() 실행화면
2. test2() 실행화면
이렇게 Json 형태로 출력된다
참고로 Map은 Json형태로 출력되지 않고 {"id" = 1, "password"="1111"} 형식으로 출력된다. List만 Json형식으로 출력된다.
또한, 이렇게 클래스로 만든 JsonObject는 Return할 때만 Json으로 출력되지, log또는 print할 경우엔 Json형식으로 출력되지 않는다.