有一段代码。
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失败,则此代码可以正常工作。
答案 0 :(得分:1)
问题是从EC2返回的Stream不支持mark。您必须将其保存到FileInputStream或ByteArrayInputStream。或者在我的情况下,将所有内容合并到亚马逊中而不将其提取到我的服务器