I m working on a spring boot application and connecting it with Amazon S3 bucket. It is a simple application for uploading videos on aws.
VideoService Class
@Service
@RequiredArgsConstructor
public class VideoService {
private final S3Service s3Service;
private final VideoRepository videoRepository;
public void uploadFile(MultipartFile file)
{
String videoURL = s3Service.uploadFile(file);
var video = new Video();
video.setVideoUrl(videoURL);
videoRepository.save(video);
}
}
Service Class
@Service
@RequiredArgsConstructor
public class S3Service implements FileService{
public static final String BUCKET_NAME = "****";
private final AmazonS3Client amazonS3Client;
@Override
public String uploadFile(MultipartFile file) {
var filenameExtension = StringUtils.getFilenameExtension(file.getOriginalFilename());
var key = UUID.randomUUID().toString() + "." + filenameExtension;
var metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());
metadata.setContentType(file.getContentType());
try {
amazonS3Client.putObject(BUCKET_NAME, key, file.getInputStream(), metadata);
} catch (IOException ioException) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR,
"An Exception occured while uploading the file");
}
amazonS3Client.setObjectAcl(BUCKET_NAME, key, CannedAccessControlList.PublicRead);
return amazonS3Client.getResourceUrl(BUCKET_NAME, key);
}
}
Controller class:
@RestController
@RequestMapping("/api/videos")
@RequiredArgsConstructor
public class VideoController {
private final VideoService videoService;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void uploadVideo(@RequestParam("file")MultipartFile file)
{
videoService.uploadFile(file);
}
}
I already created s3 bucket and access key on aws
The access key and secrey key are stored in VM options: -Dcloud.aws.credentials.access-key=**** -Dcloud.aws.credentials.secret-key=*****
this is what I get in Postman: Postman
this is shown in browser: Browser localhost
Did I miss something?
I was following a tutorial and checked every single code with the completed repo and everything was the same. Instead of giving me 200 status it is giving me 401 status.