Skip to content

Commit

Permalink
refactor: nginx
Browse files Browse the repository at this point in the history
  • Loading branch information
xxoznge committed Oct 15, 2024
1 parent 82d5dea commit 8da95c0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ddabong.ddabongdotchiBE.domain.s3;

import lombok.Builder;

@Builder
public record S3Response(
String preSignedUrl,
String imageUrl
) {
public static S3Response from(String preSignedUrl, String imageUrl) {
return S3Response.builder()
.preSignedUrl(preSignedUrl)
.imageUrl(imageUrl)
.build();
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.ddabong.ddabongdotchiBE.domain.s3.convert.webp.WebpConvertService;
Expand Down Expand Up @@ -74,4 +77,16 @@ public List<String> uploadFiles(List<MultipartFile> files) {
private String getFileNamePrefix() {
return UUID.randomUUID().toString().substring(0, 5) + "-";
}

public S3Response getPreSignedUrl(String fileName) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MINUTE, 10); //validfy of 10 minutes
fileName = s3Config.getFolder() + getFileNamePrefix() + fileName;
return S3Response.from(
amazonS3.generatePresignedUrl(s3Config.getBucket(), fileName, calendar.getTime(), HttpMethod.PUT)
.toString(),
amazonS3.getUrl(s3Config.getBucket(), fileName).toString()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -47,4 +49,9 @@ public ApiResponse<List<String>> uploadImages(
return ApiResponse.onSuccess(s3Service.uploadFiles(files));
}

@GetMapping(value = "/getPreSigned")
public S3Response getPreSignedUrl(@RequestParam String fileName) {
return s3Service.getPreSignedUrl(fileName);
}

}

0 comments on commit 8da95c0

Please sign in to comment.