-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: S3 서비스 레이어 구현
- Loading branch information
Showing
3 changed files
with
96 additions
and
25 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.hackathon.TimeLapse.s3; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequestMapping("/api/files") | ||
@RequiredArgsConstructor | ||
public class S3Controller { | ||
|
||
private final S3Service s3Service; | ||
|
||
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) | ||
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) { | ||
return s3Service.uploadFile(file); | ||
} | ||
|
||
@GetMapping("/url") | ||
public String getFileUrl(@RequestParam("fileName") String fileName) { | ||
return s3Service.getFileUrl(fileName); | ||
} | ||
|
||
@DeleteMapping("/delete") | ||
public ResponseEntity<String> deleteFile(@RequestParam("fileName") String fileName) { | ||
s3Service.deleteFile(fileName); | ||
return ResponseEntity.ok("File deleted successfully"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.hackathon.TimeLapse.s3; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
import java.util.Date; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.amazonaws.HttpMethod; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class S3Service { | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) { | ||
try { | ||
String fileName = file.getOriginalFilename(); | ||
String fileUrl = "https://" + bucket + "/test" + fileName; | ||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentType(file.getContentType()); | ||
metadata.setContentLength(file.getSize()); | ||
amazonS3Client.putObject(bucket, fileName, file.getInputStream(), metadata); | ||
return ResponseEntity.ok(fileUrl); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); | ||
} | ||
} | ||
|
||
public void deleteFile(String fileName) { | ||
amazonS3Client.deleteObject(bucket, fileName); | ||
} | ||
|
||
public String getFileUrl(String fileName) { | ||
LocalDateTime expiration = LocalDateTime.now().plusHours(1); | ||
Date expirationDate = Date.from(expiration.atZone(ZoneOffset.systemDefault()).toInstant()); | ||
|
||
GeneratePresignedUrlRequest generatePresignedUrlRequest = | ||
new GeneratePresignedUrlRequest(bucket, fileName) | ||
.withMethod(HttpMethod.GET) | ||
.withExpiration(expirationDate); | ||
|
||
URL url = amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest); | ||
return url.toString(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.