• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Spring 게시판 PreSigned URL 부분에서 CORS 에러가 나요

PreSigned URL 부분에서 CORS 에러가 나요

4 글 보임 - 1 에서 4 까지 (총 4 중에서)
  • 글쓴이
  • #125282

    박강필
    참가자
    S3Service 클래스의 동작까진 이해하고 잘 동작하는데, 
    write.html 페이지에서 임의의 이미지 파일을 선택했을 때 강의 내용처럼 S3 버킷에 저장되질 않아요.
    브라우저 콘솔을 확인해 보니 CORS 에러라고 해요.
    CORS 에러를 방지하고자 강의 내용에 나온 대로 S3 버킷에서 CORS 관련 설정에 변화를 준 게 없는데 말이에요.
    그 외에도 PUT 요청이 실패했다는 메세지와 catch 문이 없어서 put 요청을 받지 못했다고 해서
    document.addEventListener('DOMContentLoaded', (event) => {
        document.getElementById('fileInput').addEventListener('change', getURL)
    });
    async function getURL(event) {
        let file = event.target.files[0];
        if (!file) {
            console.error('No file selected');
            return;
        }
        let name = encodeURIComponent(file.name);
        try {
            let response = await fetch('/presigned-url?filename=' + name);
            if (!response.ok) throw new Error('Failed to get pre-signed URL');
            let url = await response.text();
            let uploadResponse = await fetch(url, {
               method: 'PUT',
               headers: {
                   'Content-Type': file.type
               },
               body: file
            });
            if (!uploadResponse.ok) throw new Error('Failed to upload file');
            console.log('File uploaded successfully');
            console.log(uploadResponse.url);
        } catch (error) {
            console.error(error)
        }
    }
    이렇게 코드를 짜고, 
    write.html 페이제에서
    <input type="file" id="fileInput">
    <script src="/js/imageS3upload.js" defer></script>
    이렇게 짜서 동작하게 했어요.
    근데도 여전히 CORS 문제가 해결이 안 돼요.
    이거 어떻게 하면 되나요?
     
    #125301

    codingapple
    키 마스터
    aws 사이트에서 버킷의 cors 설정 잘해놨나 확인해봅시다
    #125307

    박강필
    참가자
    [
        {
            "AllowedHeaders": [
                "*"
            ],
            "AllowedMethods": [
                "GET",
                "PUT",
                "POST",
                "DELETE",
                "HEAD"
            ],
            "AllowedOrigins": [
                "http://localhost:8080",
                "*"
            ],
            "ExposeHeaders": [
                "ETag"
            ],
            "MaxAgeSeconds": 3000
        }
    ]
    
    aws 사이트에 설정한 버킷의 cors 설정이에요
    #125316

    codingapple
    키 마스터
    버킷만들어놓은 리전 서울맞는지 application.properties에도 서울 기재했는지 버킷명도 맞는지 확인해봅시다
4 글 보임 - 1 에서 4 까지 (총 4 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관, 개인정보처리방침
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠