I’m working on Spring Boot doing a project based on REST API to upload objects to S3. I’m working on PUT request to change the file from test2.json to test4.json using Insomnia. I tried everything I could for trying to change the file using the put method but so far not been able to fix the issue since the file is showing the same as test2.json instead of test4.json. Can anyone help me fix this issue?
package com.example.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.example.demo.services.S3Service;
@RestController
@RequestMapping("/file")
public class S3Controller {
@Autowired
private S3Service s3Service;
public S3Controller(S3Service s3Service) {
this.s3Service = s3Service;
}
@GetMapping("/listAllObjects")
public ResponseEntity<List<String>> viewObjects() {
try {
List<String> objects = s3Service.listObjects();
return ResponseEntity.ok(objects);
} catch (RuntimeException ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@PostMapping("/uploadObject")
public ResponseEntity<String> uploadObject(@RequestParam("file") MultipartFile file) {
try {
s3Service.uploadObject(file);
return ResponseEntity.ok("Object uploaded successfully");
} catch (RuntimeException ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
}
}
@PutMapping("/updateObject/{objectKey}")
public ResponseEntity<String> updateObject(@PathVariable("objectKey") String objectKey, @RequestParam("file") MultipartFile file) {
try {
s3Service.updateObject(objectKey, file);
return ResponseEntity.ok("Object updated successfully");
} catch (RuntimeException ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
}
}
@DeleteMapping("/deleteObject/{objectKey}")
public ResponseEntity<String> deleteObject(@PathVariable("objectKey") String objectKey) {
try {
s3Service.deleteObject(objectKey);
return ResponseEntity.ok("Object deleted successfully");
} catch (RuntimeException ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
}
}
}
package com.example.demo.services;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.example.demo.exception.S3ServiceException;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
import software.amazon.awssdk.services.s3.model.HeadObjectResponse;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.S3Object;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class S3Service {
@Value("${aws.bucketName}")
private String bucketName;
private final S3Client s3Client;
public S3Service(S3Client s3Client) {
this.s3Client = s3Client;
}
public List<String> listObjects() {
try {
Thread.sleep(1000);
ListObjectsResponse listObjectsResponse = s3Client.listObjects(b -> b.bucket(bucketName));
return listObjectsResponse.contents().stream()
.map(S3Object::key)
.collect(Collectors.toList());
} catch (Exception ex) {
throw new S3ServiceException("Error occurred while listing S3 objects", ex);
}
}
public void uploadObject(MultipartFile file) {
try {
RequestBody requestBody = RequestBody.fromInputStream(file.getInputStream(), file.getSize());
s3Client.putObject(PutObjectRequest.builder()
.bucket(bucketName)
.key(file.getOriginalFilename())
.build(), requestBody);
} catch (IOException ex) {
throw new S3ServiceException("Error occurred while uploading object", ex);
}
}
public void updateObject(String objectKey, MultipartFile file) {
try {
try {
s3Client.headObject(HeadObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.build());
} catch (S3Exception e) {
if (e.statusCode() == 404) {
throw new S3ServiceException("Object not found");
}
throw e;
}
HeadObjectResponse existingObjectMetadata = s3Client.headObject(HeadObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.build());
long existingSize = existingObjectMetadata.contentLength();
long newSize = file.getSize();
if (existingSize == newSize) {
return;
}
RequestBody requestBody = RequestBody.fromInputStream(file.getInputStream(), file.getSize());
s3Client.putObject(PutObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.build(), requestBody);
} catch (IOException ex) {
throw new S3ServiceException("Error occurred while updating object", ex);
}
}
public void deleteObject(String objectKey) {
try {
s3Client.deleteObject(DeleteObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.build());
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
} catch (S3Exception ex) {
throw new S3ServiceException("Error occurred while deleting object", ex);
}
}