我已将客户端加密用于s3存储桶。我在Java Spring Boot中使用它。 这是我的s3存储桶配置代码:
@Bean
public AmazonS3 s3client() throws Exception {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsId, awsKey);
KeyGenerator symKeyGenerator = KeyGenerator.getInstance("AES");
symKeyGenerator.init(256);
SecretKey symKey = symKeyGenerator.generateKey();
saveSymmetricKey(masterKeyDir, masterKeyName, symKey);
symKey = loadSymmetricAESKey(masterKeyDir, masterKeyName, "AES");
AmazonS3 s3EncryptionClient = null;
try{
EncryptionMaterials encryptionMaterials = new EncryptionMaterials(symKey);
s3EncryptionClient = AmazonS3EncryptionClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withEncryptionMaterials(new StaticEncryptionMaterialsProvider(encryptionMaterials))
.withRegion(region)
.build();
}catch (Exception e){
e.printStackTrace();
}
return s3EncryptionClient;}
这是我的服务班级:
public void saveFile(MultipartFile file) throws NoSuchAlgorithmException, IOException {
try {
File convFile = convertMultiPartToFile(file);
logger.info("===================== Upload start =====================");
s3client.putObject(new PutObjectRequest(bucketName, file.getOriginalFilename(), FileUtils.openInputStream(convFile), new ObjectMetadata()));
logger.info("===================== Upload File - Done! =====================");
} catch (AmazonServiceException ase) {
logger.info("Caught an AmazonServiceException from PUT requests, rejected reasons:");
logger.info("Error Message: " + ase.getMessage());
logger.info("HTTP Status Code: " + ase.getStatusCode());
logger.info("AWS Error Code: " + ase.getErrorCode());
logger.info("Error Type: " + ase.getErrorType());
logger.info("Request ID: " + ase.getRequestId());
ase.printStackTrace();
} catch (AmazonClientException ace) {
logger.info("Caught an AmazonClientException: ");
logger.info("Error Message: " + ace.getMessage());
ace.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void downloadFile() {
java.util.Date expiration = new java.util.Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000*60*60;
expiration.setTime(expTimeMillis);
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey).withMethod(HttpMethod.GET).withExpiration(expiration);
URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest);
System.out.println(url.toString());
}
这是我使用预签名的URL打开下载的文件时得到的错误:
无法加载图像。解释JPEG图像文件时出错。