Reputation: 113
I have been trying to upload a file with the tier glacier flexible pattern, however it always comes in as the default. How can I fix this?
Follow my methods, both from a single file and from a directory
Single File
@Override
public ResponseEntity<String>uploadFiles(MultipartFile file) {
ResponseEntity<String> responseEntity = null;
try {
String data = file.getOriginalFilename().substring(0, 8);
final String fileName = data + "/" + file.getOriginalFilename();
PutObjectRequest objectRequest = PutObjectRequest.builder()
.bucket(BUCKETNAME)
.key(fileName)
.build();
s3Client.putObject(objectRequest, RequestBody.fromBytes(file.getBytes()));
responseEntity = new ResponseEntity<String>(new Gson().toJson("Upload realizado"), HttpStatus.OK);
LOGGER.info("[AwsS3BucketService] Info - Upload File: Success" );
} catch (Exception e) {
e.printStackTrace();
responseEntity = new ResponseEntity<String>(new Gson().toJson("Falha ao realizar o upload do arquivo"), HttpStatus.INTERNAL_SERVER_ERROR);
LOGGER.info("[AwsS3BucketService] Info - Upload File: Fail " + e.getMessage() );
}
return responseEntity;
}
Upload Directory
@Override
public ResponseEntity<String> uploadDirectory(String folderPath ) {
ResponseEntity<String> responseEntity = null;
//String folder = folderPath.substring(folderPath.length()-8);
try {
DirectoryUpload directoryUpload =
transferManager.uploadDirectory(UploadDirectoryRequest.builder()
.source(Paths.get(folderPath).getParent())
.bucket(BUCKETNAME)
.build());
CompletedDirectoryUpload completedDirectoryUpload = directoryUpload.completionFuture().join();
completedDirectoryUpload.failedTransfers().forEach(fail ->
LOGGER.info("Object [{}] failed to transfer", fail.toString()));
responseEntity = new ResponseEntity<String>(new Gson().toJson("Upload realizado"), HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
responseEntity = new ResponseEntity<String>(new Gson().toJson("Falha ao realizar o upload do diretório"), HttpStatus.INTERNAL_SERVER_ERROR);
LOGGER.info("[AwsS3BucketService] Info - Upload directory: Fail " + e.getMessage() );
}
return responseEntity;
}
New Method using Glacier, but i have no ideia if this approach correctly
@Override
public ResponseEntity<String> uploadDirectory(String folderPath ) {
ResponseEntity<String> responseEntity = null;
// Get an SHA-256 tree hash value.
//String checkVal = computeSHA256(myFile);
try {
UploadArchiveRequest uploadRequest = UploadArchiveRequest.builder()
.vaultName(BUCKETNAME)
//.checksum(checkVal)
.build();
UploadArchiveResponse res = glacier.uploadArchive(uploadRequest, Paths.get(folderPath));
responseEntity = new ResponseEntity<String>(new Gson().toJson("Upload realizado"), HttpStatus.OK);
} catch(GlacierException e) {
System.err.println(e.awsErrorDetails().errorMessage());
responseEntity = new ResponseEntity<String>(new Gson().toJson("Falha ao realizar o upload do diretório"), HttpStatus.INTERNAL_SERVER_ERROR);
System.exit(1);
}
return responseEntity;
}
Upvotes: 0
Views: 31