有一段代码。
List<PartETag> uploadPartsOfAsset(AssetUploadRequestVO requestVO) {
final SingleClient singleClient = clientProvider.getClient(requestVO.getAssetKind());
final List<PartETag> parts = new ArrayList<>();
final String key = String.join(DELIMITER, requestVO.getClientName(), requestVO.getAssetGroup(), requestVO.getAssetName());
final long contentSize = requestVO.getContentSize();
long position = 0;
long partSize = minPartSize;
final UploadPartRequest request = new UploadPartRequest();
try (InputStream source = requestVO.getSource()) {
for (int partNumber = requestVO.getPartNumber(); position < requestVO.getContentSize(); partNumber++) {
partSize = Math.min(partSize, (requestVO.getContentSize() - position));
final long nextFilePosition = position + partSize;
if((requestVO.getContentSize() - nextFilePosition) < minPartSize){
partSize = contentSize - position;
position = contentSize;
}
request.withBucketName(singleClient.getBucketName())
.withKey(key)
.withUploadId(requestVO.getUploadId()).withPartNumber(partNumber)
.withInputStream(source)
.withPartSize(partSize);
PartETag partETag = null;
try {
partETag = singleClient.getAmazonS3Client().uploadPart(request).getPartETag();
} catch (AmazonS3Exception e){
throw new AssetNotFoundException(e.getMessage());
}
parts.add(partETag);
position += partSize;
}
} catch (IOException e) {
throw new AssetUploadException("The asset cannot be upload.", e);
}
return parts;
}
AssetUploadRequestVO.getSource()是S3ObjectInputStream的实例。问题是此抛出异常:
com.amazonaws.SdkClientException: Unable to reset stream after calculating AWS4 signature
at com.amazonaws.auth.AWS4Signer.calculateContentHash(AWS4Signer.java:542) ~[aws-java-sdk-core-1.11.125.jar:na]
at com.amazonaws.services.s3.internal.AWSS3V4Signer.calculateContentHash(AWSS3V4Signer.java:118) ~[aws-java-sdk-s3-1.11.125.jar:na]
at com.amazonaws.auth.AWS4Signer.sign(AWS4Signer.java:213) ~[aws-java-sdk-core-1.11.125.jar:na]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1164) ~[aws-java-sdk-core-1.11.125.jar:na]
...
Caused by: java.io.IOException: Resetting to invalid mark
at java.io.BufferedInputStream.reset(BufferedInputStream.java:448) ~[na:1.8.0_144]
at com.amazonaws.internal.SdkBufferedInputStream.reset(SdkBufferedInputStream.java:106) ~[aws-java-sdk-core-1.11.125.jar:na]
at com.amazonaws.internal.SdkFilterInputStream.reset(SdkFilterInputStream.java:102) ~[aws-java-sdk-core-1.11.125.jar:na]
at com.amazonaws.event.ProgressInputStream.reset(ProgressInputStream.java:168) ~[aws-java-sdk-core-1.11.125.jar:na]
我已经找到了解决该错误的方法,但是问题是我真的不明白它为什么发生。
因此,解决方案是将S3ObjectInputStream更改为其他输入流。就我而言,我将其更改为:
new ByteArrayInputStream(IOUtils.toByteArray(requestVO.getSource()))
所以有人可以帮我解释一下问题所在吗,我的解决方案是否好?
我还要补充一点,如果我们使用S3且仅对ECS失败,则此代码可以正常工作。
最佳答案
问题是从EC2返回的Stream不支持mark。您必须将其保存到FileInputStream或ByteArrayInputStream。或者在我的情况下,将亚马逊内部的所有内容合并而不将其获取到我的服务器