Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch & store metadata for blobs #132

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/io/kestra/storage/gcs/GcsFileAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.Map;
import java.util.Optional;

@Value
Expand Down Expand Up @@ -45,4 +46,9 @@ public FileType getType() {
public long getSize() {
return blobInfo.getSize();
}

@Override
public Map<String, String> getMetadata() {
return blobInfo.getMetadata();
}
}
16 changes: 11 additions & 5 deletions src/main/java/io/kestra/storage/gcs/GcsStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.kestra.core.storages.StorageObject;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -104,6 +105,11 @@ private void parentTraversalGuard(URI uri) {

@Override
public InputStream get(String tenantId, URI uri) throws IOException {
return getWithMetadata(tenantId, uri).inputStream();
}

@Override
public StorageObject getWithMetadata(String tenantId, URI uri) throws IOException {
try {
Blob blob = this.storage.get(this.blob(tenantId, URI.create(uri.getPath())));

Expand All @@ -112,7 +118,7 @@ public InputStream get(String tenantId, URI uri) throws IOException {
}

ReadableByteChannel reader = blob.reader();
return Channels.newInputStream(reader);
return new StorageObject(blob.getMetadata(), Channels.newInputStream(reader));
} catch (StorageException e) {
throw new IOException(e);
}
Expand Down Expand Up @@ -194,16 +200,18 @@ private FileAttributes getGcsFileAttributes(Blob blob) {
}

@Override
public URI put(String tenantId, URI uri, InputStream data) throws IOException {
public URI put(String tenantId, URI uri, StorageObject storageObject) throws IOException {
try {
String path = getPath(tenantId, uri);
mkdirs(path);

BlobInfo blobInfo = BlobInfo
.newBuilder(this.blob(tenantId, uri))
.setMetadata(storageObject.metadata())
.build();

try (WriteChannel writer = this.storage.writer(blobInfo)) {
try (WriteChannel writer = this.storage.writer(blobInfo);
InputStream data = storageObject.inputStream()) {
byte[] buffer = new byte[10_240];

int limit;
Expand All @@ -212,8 +220,6 @@ public URI put(String tenantId, URI uri, InputStream data) throws IOException {
}
}

data.close();

return URI.create("kestra://" + uri.getPath());
} catch (StorageException e) {
throw new IOException(e);
Expand Down