Skip to content

Commit

Permalink
feat: preSignedUrl 생성 로직 구현 (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
choigy1001 authored Mar 20, 2024
1 parent 5da57cb commit 1a00342
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.meongcare.domain.file.s3.application;

import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3Client;
import com.meongcare.domain.file.s3.presentation.dto.response.PreSignedUrlResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

@Service
@RequiredArgsConstructor
public class PreSignedUrlService {

@Value("${cloud.aws.s3.bucket}")
private String bucket;

private final AmazonS3Client amazonS3Client;

public PreSignedUrlResponse getPreSignedUrl(String fileName) {
Calendar now = Calendar.getInstance();
now.add(Calendar.MINUTE, 3);

Date expiration = now.getTime();

String preSignedUrl = amazonS3Client.generatePresignedUrl(bucket, fileName, expiration, HttpMethod.PUT)
.toString();
return new PreSignedUrlResponse(preSignedUrl);
}

public void updateImageLink(String imageUrl) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.meongcare.domain.file.s3.presentation;

import com.meongcare.domain.file.s3.application.PreSignedUrlService;
import com.meongcare.domain.file.s3.presentation.dto.response.PreSignedUrlResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/aws/s3")
public class AwsS3Controller {

private final PreSignedUrlService preSignedUrlService;

@GetMapping
public ResponseEntity<PreSignedUrlResponse> getPreSignedUrl(@RequestParam String fileName) {
return ResponseEntity.ok(preSignedUrlService.getPreSignedUrl(fileName));
}

@PatchMapping
public ResponseEntity<Void> updateImageLink(@RequestParam String imageUrl) {
preSignedUrlService.updateImageLink(imageUrl);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.meongcare.domain.file.s3.presentation.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class PreSignedUrlResponse {
private String preSignedUrl;
}

0 comments on commit 1a00342

Please sign in to comment.