【发布时间】:2022-09-25 17:03:18
【问题描述】:
在我们的应用程序中,我们有应该保护的附件。这些附件存储在 DO Spaces 中,我们正在生成已签名的 URL 以允许安全访问。
签署 URL 的逻辑如下所示:
String getSignUrl(String objectKey, Long expirationMillis) {
String signedUrl = null;
try {
AmazonS3 s3Client = awsClientProviderService.getS3Client()
// Set the presigned URL to expire after one hour.
Date expiration = new Date()
Long expTimeMillis = expiration.getTime();
expTimeMillis += expirationMillis;
expiration.setTime(expTimeMillis);
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(spaceName, objectKey)
.withMethod(HttpMethod.GET)
.withExpiration(expiration);
URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
signedUrl = url.toString()
} catch (AmazonServiceException e) {
log.error(e.message, e)
} catch (SdkClientException e) {
log.error(e.message, e)
}
signedUrl
}
其中getS3Client 是:
AmazonS3 getS3Client() {
if (!s3Client) {
AWSStaticCredentialsProvider credentials =
new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secret))
s3Client = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region))
.withCredentials(credentials)
.build()
}
s3Client
}
到目前为止一切都很好,而且效果很好。
但是,当我们开始对 URL 进行签名时,在 50 个这样的请求之后,我们会收到以下错误:
Unable to execute HTTP request: Timeout waiting for connection from pool
稍微挖掘了一下,我尝试将maxConnections 添加到客户端:
.withClientConfiguration(new ClientConfiguration().withMaxConnections(20))
这导致在 20 次此类请求后出现错误。顺便说一句,即使我们先签署 5 个 URL,然后等待 10 分钟,再尝试另一批。该错误发生在总共 20 个请求之后。
试过了,100个连接。 100 个请求后的相同故事。
这让我觉得连接由于某种原因没有被释放。我唯一能找到的是人们提议将maxConnections 增加到 1000。但是,这只会推迟问题。
这里的解决方案是什么?我们如何释放 S3Client 的连接?
标签: java digital-ocean aws-java-sdk digital-ocean-spaces