Skip to content

Instantly share code, notes, and snippets.

@mmafrar
Created January 15, 2021 15:48
Show Gist options
  • Save mmafrar/889733cb2c767fde44814aaf1b98a942 to your computer and use it in GitHub Desktop.
Save mmafrar/889733cb2c767fde44814aaf1b98a942 to your computer and use it in GitHub Desktop.
Integrating your Spring Boot project with Amazon S3
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