Skip to content

Commit

Permalink
Merge pull request #10 from UMC-7-Time-Lapse/feat/#9
Browse files Browse the repository at this point in the history
feat: S3 서비스 레이어 구현
  • Loading branch information
oxdjww authored Jul 4, 2024
2 parents 032cf42 + 9c5e7ce commit 9fc6bab
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 25 deletions.
33 changes: 33 additions & 0 deletions src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java
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");
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/hackathon/TimeLapse/s3/S3Service.java
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();
}
}
25 changes: 0 additions & 25 deletions src/main/resources/application-local.yml

This file was deleted.

0 comments on commit 9fc6bab

Please sign in to comment.