Skip to content

Commit e5505bc

Browse files
authored
Retrieve MD5 from object metadata (#414)
* Retrieve MD5 from object metadata * Decode Base64 formatted contentMD5 value
1 parent 216edc9 commit e5505bc

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

score-server/src/main/java/bio/overture/score/server/config/S3Config.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class S3Config {
3939
private boolean sigV4Enabled;
4040
private String masterEncryptionKeyId;
4141

42+
@Value("md5chksum")
43+
private String customMd5Property;
44+
4245
@Value("${upload.connection.timeout}")
4346
private int connectionTimeout;
4447

score-server/src/main/java/bio/overture/score/server/repository/s3/S3DownloadService.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import bio.overture.score.core.model.ObjectKey;
2424
import bio.overture.score.core.model.ObjectSpecification;
2525
import bio.overture.score.core.model.Part;
26+
import bio.overture.score.core.util.MD5s;
2627
import bio.overture.score.core.util.ObjectKeys;
2728
import bio.overture.score.core.util.PartCalculator;
29+
import bio.overture.score.server.config.S3Config;
2830
import bio.overture.score.server.exception.IdNotFoundException;
2931
import bio.overture.score.server.exception.InternalUnrecoverableError;
3032
import bio.overture.score.server.exception.NotRetryableException;
@@ -89,6 +91,7 @@ public class S3DownloadService implements DownloadService {
8991
@Autowired private URLGenerator urlGenerator;
9092
@Autowired private PartCalculator partCalculator;
9193
@Autowired private MetadataService metadataService;
94+
@Autowired private S3Config s3config;
9295

9396
@Override
9497
public ObjectSpecification download(
@@ -118,14 +121,17 @@ public ObjectSpecification download(
118121
parts = partCalculator.divide(offset, length);
119122
}
120123
fillPartUrls(objectKey, parts, false, forExternalUse);
124+
125+
val md5 = getObjectMd5(metadata);
126+
121127
objectSpec =
122128
new ObjectSpecification(
123129
objectKey.getKey(),
124130
objectId,
125131
objectId,
126132
parts,
127133
metadata.getContentLength(),
128-
metadata.getETag(),
134+
md5,
129135
false);
130136
}
131137

@@ -189,6 +195,35 @@ public ObjectSpecification download(
189195
}
190196
}
191197

198+
/**
199+
* Looks for MD5 hash in the object metadata. This is used as part of the fallback behaviour when
200+
* the .meta file cannot be found. To find the MD5, we will look for a value using the built in S3
201+
* getContendMD5(), and if no value is found there we will check in a configurable user meta data
202+
* property. The name of this property is configurable via the S3 Config. If no MD5 value can be
203+
* found in either of these locations then it will be returned null.
204+
*
205+
* <p>A user can still download files with the MD5 set to null, but they will always fail to
206+
* validate through the CLI. To complete a download of a file in this state, the user should add
207+
* the argument to their CLI download command: --validate false
208+
*
209+
* @param metadata
210+
* @return
211+
*/
212+
private String getObjectMd5(ObjectMetadata metadata) {
213+
val contentMd5 = metadata.getContentMD5();
214+
if (contentMd5 != null) {
215+
return MD5s.toHex(contentMd5);
216+
}
217+
val userMetadataMd5 =
218+
metadata.getUserMetaDataOf(s3config.getCustomMd5Property()); // get literal from config
219+
if (userMetadataMd5 != null) {
220+
return MD5s.toHex(userMetadataMd5);
221+
}
222+
223+
// No value found, returning null.
224+
return null;
225+
}
226+
192227
private static ObjectSpecification removeUrls(ObjectSpecification spec) {
193228
spec.getParts().forEach(x -> x.setUrl(null));
194229
return spec;

score-server/src/main/resources/application.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ s3:
3434
secured: true
3535
sigV4Enabled: true
3636

37+
# custom meta property with md5 hash, unused when upload state files are available (default behaviour)
38+
# customMd5Property: md5chksum
39+
3740
#amazon
3841
endpoint: s3-external-1.amazonaws.com
3942

0 commit comments

Comments
 (0)