1. 기존의 S3에 업로드된 파일을 다운로드 할것이다.

  2. 그렇게 하기 위해선 일단 다운로드 버튼에서 파일의 기본적인 값을 넘겨줘야함

        const imgDownloadForm = (btn) => {
            // data-* 속성에서 값 추출 (경로, 이름)
            const objectKey = btn.getAttribute('data-key');
            const fileName = btn.getAttribute('data-filename');
            console.log(objectKey);
    
            // Presigned URL을 받아오는 Ajax 요청
            fetch('/successcase/download?objectKey=' + encodeURIComponent(objectKey) + '&fileName=' + encodeURIComponent(fileName))
                .then(response => {
                    if (!response.ok) throw new Error('Presigned URL 요청 실패');
                    return response.text(); // Presigned URL이 문자열로 리턴됨
                })
                .then(presignedUrl => {
                    window.open(presignedUrl, '_blank'); // 새창에서 다운로드 진행
                })
                .catch(err => {
                    alert('다운로드 중 오류가 발생했습니다: ' + err.message);
                });
        };
    
  3. 그리고 나서 아마존 S3를 관리하는 유틸 클래스에 다운로드 함수 추가

    public static String generateDownloadUrl(String bucketName, String objectKey, String region, String accessKey, String secretKey, String fileName) {
            // 1. S3 클라이언트 생성
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(region)
                    .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
                    .build();
    
            // 2. 만료 시간 설정 (예: 1분)
            Date expiration = new Date();
            long expTimeMillis = expiration.getTime();
            expTimeMillis += 1000 * 60; // 1분
            expiration.setTime(expTimeMillis);
    
            // 3. Presigned URL 생성 요청 객체 생성
            GeneratePresignedUrlRequest generatePresignedUrlRequest =
                    new GeneratePresignedUrlRequest(bucketName, objectKey)
                            .withMethod(HttpMethod.GET)
                            .withExpiration(expiration);
    
            // 4. 반드시 다운로드 헤더 추가!
            generatePresignedUrlRequest.addRequestParameter(
                    "response-content-disposition", "attachment; filename=\"" + fileName + "\""
            );
    
            // 5. Presigned URL 생성
            URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
    
            return url.toString();
        }
    

  1. S3 클라이언트 생성 (AmazonS3 s3Client)
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withRegion(region)
                .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
                .build();

  1. 만료 시간 설정 (expiration)
Date expiration = new Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60; // 1분
expiration.setTime(expTimeMillis);