Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit 9a434b8

Browse files
committed
S3MessageHandler: Don't close stream after MD5
Related to https://stackoverflow.com/questions/65892759/for-an-upload-inputstream-with-no-md5-digest-metadata-the-marksupported-meth The `Md5Utils.md5AsBase64(inputStream)` closes an `InputStream` in the end, but we still need this `InputStream` as an original data for the remote file to store * Change `S3MessageHandler` logic to use a `DigestUtils.md5Digest(inputStream)` instead which doesn't close `InputStream` in the end
1 parent ea13591 commit 9a434b8

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/main/java/org/springframework/integration/aws/outbound/S3MessageHandler.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import org.springframework.messaging.MessageHeaders;
3434
import org.springframework.messaging.support.MessageBuilder;
3535
import org.springframework.util.Assert;
36+
import org.springframework.util.DigestUtils;
3637

3738
import com.amazonaws.AmazonClientException;
3839
import com.amazonaws.event.ProgressEvent;
@@ -53,6 +54,7 @@
5354
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
5455
import com.amazonaws.services.s3.transfer.internal.S3ProgressListener;
5556
import com.amazonaws.services.s3.transfer.internal.S3ProgressListenerChain;
57+
import com.amazonaws.util.Base64;
5658
import com.amazonaws.util.Md5Utils;
5759

5860
/**
@@ -103,6 +105,7 @@
103105
*
104106
* @author Artem Bilan
105107
* @author John Logan
108+
*
106109
* @see TransferManager
107110
*/
108111
public class S3MessageHandler extends AbstractReplyProducingMessageHandler {
@@ -326,7 +329,7 @@ private Transfer upload(Message<?> requestMessage) {
326329
this.uploadMetadataProvider.populateMetadata(metadata, requestMessage);
327330
}
328331

329-
PutObjectRequest putObjectRequest = null;
332+
PutObjectRequest putObjectRequest;
330333

331334
try {
332335
if (payload instanceof InputStream) {
@@ -335,8 +338,8 @@ private Transfer upload(Message<?> requestMessage) {
335338
Assert.state(inputStream.markSupported(),
336339
"For an upload InputStream with no MD5 digest metadata, "
337340
+ "the markSupported() method must evaluate to true.");
338-
String contentMd5 = Md5Utils.md5AsBase64(inputStream);
339-
metadata.setContentMD5(contentMd5);
341+
byte[] md5Digest = DigestUtils.md5Digest(inputStream);
342+
metadata.setContentMD5(Base64.encodeAsString(md5Digest));
340343
inputStream.reset();
341344
}
342345
putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata);

0 commit comments

Comments
 (0)