我有一个应用程序,它是一个数字资产管理系统。它显示缩略图。我将这些缩略图设置为可与AWS S3预签名URL一起使用:https ://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURLJavaSDK.html 。这段代码一直有效,直到我更改了通过请求处理的项目数量。该应用程序具有25、50、100、200的选择。如果我选择100或200,则该过程将失败,并显示“错误:com.amazonaws.AmazonServiceException:请求过多(服务:null;状态代码:429;错误代码:null ;请求ID:null)“
现在的过程如下:执行搜索>通过返回该对象的预签名URL的方法运行每个对象键。
我们通过Elastic Container Service运行此应用程序,该服务允许我们通过ContainerCredentialsProvider提取凭据。
相关代码进行审查:
String s3SignedUrl(String objectKeyUrl) {
// Environment variables for S3 client.
String clientRegion = System.getenv("REGION");
String bucketName = System.getenv("S3_BUCKET");
try {
// S3 credentials get pulled in from AWS via ContainerCredentialsProvider.
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(clientRegion)
.withCredentials(new ContainerCredentialsProvider())
.build();
// Set the pre-signed URL to expire after one hour.
java.util.Date expiration = new java.util.Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 60;
expiration.setTime(expTimeMillis);
// Generate the presigned URL.
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucketName, objectKeyUrl)
.withMethod(HttpMethod.GET)
.withExpiration(expiration);
return s3Client.generatePresignedUrl(generatePresignedUrlRequest).toString();
} catch (AmazonServiceException e) {
throw new AssetException(FAILED_TO_GET_METADATA, "The call was transmitted successfully, but Amazon " +
"S3 couldn't process it, so it returned an error response. Error: " + e);
} catch (SdkClientException e) {
throw new AssetException(FAILED_TO_GET_METADATA, "Amazon S3 couldn't be contacted for a response, or " +
"the client couldn't parse the response from Amazon S3. Error: " + e);
}
}
这是我们处理项目的部分:
// Overwrite the url, it's nested deeply in maps of maps.
for (Object anAssetList : assetList) {
String assetId = ((Map) anAssetList).get("asset_id").toString();
if (renditionAssetRecordMap.containsKey(assetId)) {
String s3ObjectKey = renditionAssetRecordMap.get(assetId).getThumbObjectLocation();
((Map) ((Map) ((Map) anAssetList)
.getOrDefault("rendition_content", new HashMap<>()))
.getOrDefault("thumbnail_content", new HashMap<>()))
.put("url", s3SignedUrl(s3ObjectKey));
}
}
任何指导将不胜感激。希望能在AWS端实现简单且可配置的解决方案。否则,现在我正在考虑为此添加一个过程以批量生成网址。
该问题与生成预签名URL无关。这些都是在不与服务交互的情况下完成的,因此没有任何可能的方法来限制其速率。预签名URL使用HMAC-SHA算法向服务证明拥有凭证的实体已授权特定请求。HMAC-SHA的单向(不可逆)性质允许这些URL完全在运行代码的计算机上生成,而无需服务交互。
但是,很可能反复获取凭证是导致异常的真正原因-而且您似乎一遍又一遍地不必要地这样做。
这是一项昂贵的操作:
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(clientRegion)
.withCredentials(new ContainerCredentialsProvider())
.build();
每次再次调用时,都必须再次获取凭据。那实际上是您要达到的极限。
s3client
仅构建一次,然后重构s3SignedUrl()
以期望该对象被传递,因此您可以重用它。
除了解决429
错误之外,您还应该看到显着的性能改进。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句