일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- java참조자료형
- java map
- java list 출력
- java 자료구조 활용
- 작은수제거하기
- 격파르타장점
- java기본자료형
- java알고리즘문제풀이
- java map 저장
- 프로그래머스제일작은수
- 인터프린터언어
- java set 출력
- 격파르타후기
- java list 저장
- 격파르타비전공자
- java map 출력
- 격파르타합격후기
- 프로그래머스
- 컴파일
- javaJVM
- java알고리즘
- 항해99후기
- 항해15기
- java set 저장
- 노베이스부트캠프
- sqld자격증합격
- 코딩부트캠프후기
- 비전공자sqld
- javaJRE
- java최솟값구하기
- Today
- Total
코딩과 결혼합니다
230718 - Spring 기능 구현 하며 에러 해결 본문
1.The bucket does not allow ACLs
이유 : 버킷의 ACL권한을 안줘서 생긴 오류
해결
S3 - 권한 - ACL 권한 부여
2.Index 1 out of bounds for length 1
이유 : 지금 만들고 있는 프로젝트의 주제가 '집에서 만드는 인생네컷' 으로 사진을 4개 보내야 하는데 1개만 보내줬기 때문
해결 : 사진 4개를 보내주었다.
그리고, 첫 번째 줄 처럼 json형식으로 함께 포함해서 보내주지 않으면 오류가 생겼다.
3.Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
이유 : MessageResponseDto 를 출력값으로 내보내고 있는데 Getter를 쓰지 않아서 데이터를 받아오지 못함
해결 : MessageResponseDto에 @Getter 추가. (오류는 떴지만 사진은 잘 저장되고 있었다.)
4.업로드 부분까지 구현하였을 때 get을 하면 생기는 문제
전체 URL이 아닌 일부 URL만 저장되어 사진을 불러올 수 없다.
해결
PostService
//업로드
public MessageResponseDto upload(PostRequestDto postRequestDto, List<MultipartFile> photos) throws IOException {
validateFileCounts(photos);
List<String> uuidFilePaths = new ArrayList<>();
for (MultipartFile photo : photos) {
long size = photo.getSize();
ObjectMetadata objectMetaData = new ObjectMetadata();
objectMetaData.setContentType(photo.getContentType());
objectMetaData.setContentLength(size);
String prefix = UUID.randomUUID().toString();
String fileName = prefix + "_" + photo.getOriginalFilename();
String bucketFilePath = "photos/" + fileName;
// S3에 업로드
amazonS3Client.putObject(
new PutObjectRequest(bucketName, bucketFilePath, photo.getInputStream(), objectMetaData)
.withCannedAcl(CannedAccessControlList.PublicRead)
);
uuidFilePaths.add(fileName);
}
Post post = new Post(postRequestDto, uuidFilePaths);
postRepository.save(post);
return new MessageResponseDto("파일 저장 성공");
}
코드 부분 해석
String prefix = UUID.randomUUID().toString();
String fileName = prefix + "_" + photo.getOriginalFilename();
String bucketFilePath = "photos/" + fileName;
-------------------------------
uuidFilePaths.add(fileName);
UUID로 같은 파일이여도 유니크한 id값을 앞에 붙여줌으로 각각 다른 파일로 취급할 수 있음.
마지막 줄은 "photos/"를 해줌으로 그냥 저장되던 사진들이 photos/ 파일에 저장된다.
다음 .add(fileName)을 해줘서 photos/를 뺀 UUID + filename 만 저장해준다.
PostResponseDto
public PostResponseDto(Post post){
this.id = post.getId();
this.username = post.getUsername();
this.title = post.getTitle();
this.content = post.getContent();
this.photo_one = fileNameToURL(post.getPhoto_one());
this.photo_two = fileNameToURL(post.getPhoto_two());
this.photo_three = fileNameToURL(post.getPhoto_three());
this.photo_four = fileNameToURL(post.getPhoto_four());
this.createdAt = post.getCreatedAt();
System.out.println("시간: " + this.createdAt);
}
private String fileNameToURL(String fileName){
return "https://chaewon-project-bucket.s3.ap-northeast-2.amazonaws.com/photos/" + fileName;
}
완전한 url로 만들어 주기 위해서 배포할 url의 앞부분을 붙여주었다.
이제 저 링크를 클릭하면 사진이 잘 보여진다.
5. createdAt이 배열형태로 나옴
이유 : java8까지는 이런 형태로 나왔다는데 설정이 중간에 꼬인 것같다.확인해 보았을때 문제는 없어보였다.
해결
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
6. 머지를 하면 젠킨스에서 CI/CD를(자동배포) 해주는데 안 됨.
이유 : 레포지토리가 private인 경우에는 git webhook이 정상적으로 동작하지 못함. jenkins에서 레포지토리 권한을 얻을 수 없음
해결 : 레포지토리를 pulic으로 바꿔줌 (공유되면 안되는 정보를 내보내서 임시적으로 private을 해놓았었다.)
'2세 > Spring' 카테고리의 다른 글
230720 - Error: Required a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' that could not be found(2) (0) | 2023.07.20 |
---|---|
230719 - RestTemplate & Open API (0) | 2023.07.19 |
230711 - Spring : passwordEncoder 오토와이어링 오류 (0) | 2023.07.11 |
230710 - Spring : JPA 와ORM (0) | 2023.07.10 |
230709 - Spring @EntityListeners(AuditingEntityListener.class) (0) | 2023.07.10 |