투케이2K

402. (javaScript) 자바스크립트 AWS S3 Get 요청 및 Put 업로드 PreSignedUrl 프리 사인 URL 주소 생성 수행 - getSignedUrl 본문

JavaScript

402. (javaScript) 자바스크립트 AWS S3 Get 요청 및 Put 업로드 PreSignedUrl 프리 사인 URL 주소 생성 수행 - getSignedUrl

투케이2K 2025. 6. 18. 19:05
728x90
반응형

[개발 환경 설정]

개발 툴 : Edit++

개발 언어 : JavaScript

 

[소스 코드]

-----------------------------------------------------------------------------------------
[사전 설명 및 설정 사항]
-----------------------------------------------------------------------------------------

- 개발 환경 : Web

- 개발 기술 : JavaScript (자바스크립트) / AWS / S3 / PreSignedUrl / Get Api / Put Api

-----------------------------------------------------------------------------------------





-----------------------------------------------------------------------------------------
[소스 코드]
-----------------------------------------------------------------------------------------

<!DOCTYPE HTML>
<html lang="ko">
<head>
    <title>javaScriptTest</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">


    <!-- 반응형 구조 만들기 -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">


    <!-- 내부 CSS 스타일 지정 -->
    <style>

        html, body {
            width: 100%;
            height: 100%;
            margin : 0 auto;
            padding : 0;
            border : none;
            background-color: #666;
        }

    </style>





    <!-- [CDN 주소 설정] -->
    <script src="https://code.jquery.com/jquery-latest.min.js"></script>
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.1416.0.min.js"></script>






    <!-- [자바스크립트 코드 지정] -->
    <script>

        // --------------------------------------------------------------------------------------------------------------

        // [전역 변수 선언]
        var region = 'ap-northeast-2'; // [AWS 리전]
        var accessKeyId = 'AK..7Q'; // [IAM 액세스 키]
        var secretAccessKey = 'Zz..xj'; // [IAM 시크릿 키]

        // --------------------------------------------------------------------------------------------------------------

        // [html 최초 로드 및 이벤트 상시 대기 실시]
        window.onload = async function() {
            console.log("-");
            console.log("=========================================");
            console.log("[window onload] : [start]");
            console.log("=========================================");
            console.log("-");


            // -----------------------------------------
            // [AWS.config 지정]
            // -----------------------------------------
            AWS.config.update({
              region: region,
              accessKeyId: accessKeyId,
              secretAccessKey: secretAccessKey,
              signatureVersion: "v4", // [API 요청용 AWS Signature Version 4]
            });



            // -----------------------------------------
            // [AWS.S3 객체 생성]
            // -----------------------------------------
            const s3 = new AWS.S3();
            


            // -----------------------------------------
            // [GET 확인 용도 : Pre-Signed URL 생성 요청]
            // -----------------------------------------
            // https://service.s3.ap-northeast-2.amazonaws.com/control/private.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AK..7Q%2F20250618%2Fap-northeast-2%2Fs3%2Faws4_request&X-Amz-Date=20250618T000124Z&X-Amz-Expires=3600&X-Amz-Signature=bd..86&X-Amz-SignedHeaders=host
            // -----------------------------------------
            const getExpiresInSeconds = 3600; // 만료 시간 (3600 초 : 1시간) 
            const getParams = {
              Bucket: 'service', // 버킷 이름
              Key: 'control/private.txt', // [폴더 및 파일 경로]
              Expires: getExpiresInSeconds // URL 유효 시간 (초)
            }; 

            const getUrl = await s3.getSignedUrl('getObject', getParams); // ------> getObject

            console.log("-");
            console.log("=========================================");
            console.log("[Get - PreSignedUrl] : [Success]");
            console.log("---------------------------------------");
            console.log("url :: " + getUrl); 
            console.log("=========================================");
            console.log("-");


            // -----------------------------------------
            // [PUT 업로드 용도 : Pre-Signed URL 생성 요청]
            // -----------------------------------------
            // https://service.s3.ap-northeast-2.amazonaws.com/control/photo.jpg?Content-Type=image%2Fjpeg&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AK..7Q%2F20250618%2Fap-northeast-2%2Fs3%2Faws4_request&X-Amz-Date=20250618T001010Z&X-Amz-Expires=3600&X-Amz-Signature=9e..70&X-Amz-SignedHeaders=host
            // -----------------------------------------
            const putExpiresInSeconds = 3600; // 만료 시간 (3600 초 : 1시간) 
            const putParams = {
              Bucket: 'service\', // 버킷 이름
              Key: 'control/photo.jpg', // [폴더 및 파일 경로]
              Expires: putExpiresInSeconds, // URL 유효 시간 (초)
              ContentType: "image/jpeg", // 업로드할 파일 타입
            }; 

            const putUrl = await s3.getSignedUrl('putObject', putParams); // ------> putObject

            console.log("-");
            console.log("=========================================");
            console.log("[Put - PreSignedUrl] : [Success]");
            console.log("---------------------------------------");
            console.log("url :: " + putUrl); 
            console.log("=========================================");
            console.log("-");


            // -----------------------------------------
            // [Body 표시 JSON]
            // -----------------------------------------
            var resJson = {
              respones: "success",
              data: {
                getPreSignedUrl: getUrl,
                putPreSignedUrl: putUrl
              }
            }


            //document.body.innerHTML = JSON.stringify(resJson); // [1 표시]
            document.write(JSON.stringify(resJson)); // [2  표시]

        };

        // --------------------------------------------------------------------------------------------------------------

    </script>


</head>


<body>

</body>

</html>

-----------------------------------------------------------------------------------------





-----------------------------------------------------------------------------------------
[참고 사이트]
-----------------------------------------------------------------------------------------

[Aws S3 Storage] S3 (Amazon Simple Storage Service) 버킷 저장소 개념 및 설명 정리

https://blog.naver.com/kkh0977/223733087281?trackingCode=blog_bloghome_searchlist


[[간단 소스] Aws S3 버킷 저장소 리스트 목록 확인 - AmazonS3 listBuckets]

https://blog.naver.com/kkh0977/223797258160?trackingCode=blog_bloghome_searchlist


[자바스크립트 AWS S3 getSignedUrl 사용해 버킷에 저장 된 객체 일시적 다운로드 URL 생성 수행]

https://blog.naver.com/kkh0977/223902550683

-----------------------------------------------------------------------------------------
 
728x90
반응형
Comments