Skip to content

Commit

Permalink
fix: list properly returns metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-mulier-p committed Dec 9, 2024
1 parent 9ed5930 commit 5a5e1d2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
31 changes: 31 additions & 0 deletions src/main/java/io/kestra/storage/s3/MetadataUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.kestra.storage.s3;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class MetadataUtils {
private static final Pattern METADATA_KEY_WORD_SEPARATOR = Pattern.compile("_([a-z])");
private static final Pattern UPPERCASE = Pattern.compile("([A-Z])");

public static Map<String, String> toStoredMetadata(Map<String, String> metadata) {
if (metadata == null) {
return null;
}
return metadata.entrySet().stream()
.map(entry -> Map.entry(UPPERCASE.matcher(entry.getKey()).replaceAll("_$1").toLowerCase(), entry.getValue()))
.collect(HashMap::new, (m, v) -> m.put(v.getKey(), v.getValue()), HashMap::putAll);
}

public static Map<String, String> toRetrievedMetadata(Map<String, String> metadata) {
if (metadata == null) {
return null;
}
return metadata.entrySet().stream()
.map(entry -> Map.entry(
METADATA_KEY_WORD_SEPARATOR.matcher(entry.getKey())
.replaceAll(matchResult -> matchResult.group(1).toUpperCase()),
entry.getValue()
)).collect(HashMap::new, (m, v) -> m.put(v.getKey(), v.getValue()), HashMap::putAll);
}
}
14 changes: 11 additions & 3 deletions src/main/java/io/kestra/storage/s3/S3FileAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@
import java.util.Map;

@Value
@Builder
public class S3FileAttributes implements FileAttributes {

String fileName;
HeadObjectResponse head;
boolean isDirectory;
Map<String, String> metadata;

@Builder
public S3FileAttributes(String fileName, HeadObjectResponse head, boolean isDirectory) {
this.fileName = fileName;
this.head = head;
this.isDirectory = isDirectory;

this.metadata = MetadataUtils.toRetrievedMetadata(head.metadata());
}

@Override
public long getLastModifiedTime() {
Expand Down Expand Up @@ -48,6 +56,6 @@ public long getSize() {

@Override
public Map<String, String> getMetadata() {
return head.metadata();
return metadata;
}
}
24 changes: 2 additions & 22 deletions src/main/java/io/kestra/storage/s3/S3Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public StorageObject getWithMetadata(String tenantId, @Nullable String namespace
resultInputStream = InputStream.nullInputStream();
}

return new StorageObject(toRetrievedMetadata(result.response().metadata()), resultInputStream);
return new StorageObject(MetadataUtils.toRetrievedMetadata(result.response().metadata()), resultInputStream);
}catch (ExecutionException e) {
if (e.getCause() instanceof S3Exception s3Exception && s3Exception.statusCode() == 404) {
throw new FileNotFoundException();
Expand Down Expand Up @@ -251,7 +251,7 @@ public URI put(String tenantId, @Nullable String namespace, URI uri, StorageObje
PutObjectRequest request = PutObjectRequest.builder()
.bucket(this.getBucket())
.key(path)
.metadata(toStoredMetadata(storageObject.metadata()))
.metadata(MetadataUtils.toStoredMetadata(storageObject.metadata()))
.build();

Optional<Upload> upload;
Expand Down Expand Up @@ -461,24 +461,4 @@ private void parentTraversalGuard(URI uri) {
private static URI createUri(String tenantId, String key) {
return URI.create("kestra://%s".formatted(key).replace(tenantId + "/", ""));
}

private Map<String, String> toStoredMetadata(Map<String, String> metadata) {
if (metadata == null) {
return null;
}
return metadata.entrySet().stream()
.map(entry -> Map.entry(UPPERCASE.matcher(entry.getKey()).replaceAll("_$1").toLowerCase(), entry.getValue()))
.collect(HashMap::new, (m, v) -> m.put(v.getKey(), v.getValue()), HashMap::putAll);
}
private Map<String, String> toRetrievedMetadata(Map<String, String> metadata) {
if (metadata == null) {
return null;
}
return metadata.entrySet().stream()
.map(entry -> Map.entry(
METADATA_KEY_WORD_SEPARATOR.matcher(entry.getKey())
.replaceAll(matchResult -> matchResult.group(1).toUpperCase()),
entry.getValue()
)).collect(HashMap::new, (m, v) -> m.put(v.getKey(), v.getValue()), HashMap::putAll);
}
}

0 comments on commit 5a5e1d2

Please sign in to comment.