Created
January 15, 2021 15:48
-
-
Save mmafrar/889733cb2c767fde44814aaf1b98a942 to your computer and use it in GitHub Desktop.
Integrating your Spring Boot project with Amazon S3
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
package com.example.demo; | |
import com.example.demo.FileService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.core.io.InputStreamResource; | |
import org.springframework.http.CacheControl; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.*; | |
import org.springframework.web.multipart.MultipartFile; | |
import java.util.Map; | |
@RestController | |
@RequestMapping("/api/v1/files") | |
@CrossOrigin(origins = "*", maxAge = 3600) | |
public class FileController { | |
private static final String MESSAGE_1 = "Uploaded the file successfully"; | |
private static final String FILE_NAME = "fileName"; | |
@Autowired | |
FileService fileService; | |
@GetMapping | |
public ResponseEntity<Object> findByName(@RequestBody(required = false) Map<String, String> params) { | |
return ResponseEntity | |
.ok() | |
.cacheControl(CacheControl.noCache()) | |
.header("Content-type", "application/octet-stream") | |
.header("Content-disposition", "attachment; filename=\"" + params.get(FILE_NAME) + "\"") | |
.body(new InputStreamResource(fileService.findByName(params.get(FILE_NAME)))); | |
} | |
@PostMapping | |
public ResponseEntity<Object> save(@RequestParam("file") MultipartFile multipartFile) { | |
fileService.save(multipartFile); | |
return new ResponseEntity<>(MESSAGE_1, HttpStatus.OK); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment