I am a Java engineer designing an "Aws S3" utility class.
I now need to upload files in bulk, so in the upload method "uploadFile", I have commented out the close connection statement in "finally".
I want to keep this connection multiplexed to avoid the overhead of re-establishing a connection, so I set up a Object pool,the object is Connection.
Does AmazonS3 automatically disconnect after being idle for a long time?
If the connection presence is automatically closed, will the s3Client in the ClientPool become null, resulting in a null value corresponding to the key?
How can I solve these problems? Please help to correct my code, thank you.If you know Chinese, please refer to the original Chinese question to avoid ambiguity.
The code is as follows:
private static Map<String, AmazonS3> ClientPool = new ConcurrentHashMap<>();
/**
* 获取 Amazon S3 客户端实例
* @param region
* @param accessKeyId
* @param secretKey
* @param endpointUrl
* @return 连接池复用的client
*/
public static AmazonS3 getS3Client(String region, String accessKeyId, String secretKey, String endpointUrl) {
String clientKey = region + accessKeyId + secretKey + endpointUrl;
return ClientPool.computeIfAbsent(clientKey, k -> createS3Client(region, accessKeyId, secretKey, endpointUrl));
}
// todo aws服务端自动空闲断连,不用扫描。
/**
* S3 建立连接
*
* @param region 时区
* @param accessKeyId 用户私钥
* @param secretKey 公钥
* @param endpointUrl 节点位置
* @return 已连接
*/
public static AmazonS3 createS3Client(String region, String accessKeyId, String secretKey, String endpointUrl) {
region = StringUtils.isEmpty(region) ? "us-east-1" : region;
AwsClientBuilder.EndpointConfiguration endpointConfig = new AwsClientBuilder.EndpointConfiguration(endpointUrl, region);
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKeyId, secretKey);
AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTPS);
AmazonS3 s3client = AmazonS3Client.builder().withEndpointConfiguration(endpointConfig).withClientConfiguration(clientConfig).withCredentials(awsCredentialsProvider).disableChunkedEncoding().withPathStyleAccessEnabled(true).withForceGlobalBucketAccessEnabled(true).build();
return s3client;
}
/**
* AWS s3 上传
*
* @param targetPullFileName 存放文件 example:"path/path/xx.csv"
* @param inputStream 文件输入流
* @return
*/
public String uploadFile(String targetPullFileName, InputStream inputStream) throws IOException {
log.info("=======================================开始上传文件=======================================");
log.info("Upload File: " + targetPullFileName + " To " + bucketName);
AmazonS3 s3Client = getS3Client(region, accessKeyId, secretKey, endpointUrl);
try {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(inputStream.available());
PutObjectRequest request = new PutObjectRequest(bucketName, targetPullFileName, inputStream, metadata);
s3Client.putObject(request);
} catch (Exception e) {
log.error("上传" + targetPullFileName + "文件失败,错误原因为" + e.getMessage());
targetPullFileName += "上传失败";
e.printStackTrace();
} finally {
inputStream.close();
// s3Client.shutdown();
return targetPullFileName;
}
}
我是Java工程师,正在设计一个 ”Aws S3 “工具类。 我现在批量上传文件,所以在上传方法中,我最后注释掉了关闭连接的语句。我想保持这个连接复用以避免重新建立连接的开销,因此我设置了一个连接池。“AmazonS3”在长时间空闲后是否会自动断开连接。如果连接存在被自动关闭了,ClientPool中的s3Client是否会变成null,从而导致与该键对应的值为null?请帮我指出我代码中的问题。
Thank you for your reply. I found the relevant details in the official document later. It's really a great design. Thank you again for your answer