Skip to content

Commit

Permalink
fix: check for the directory existance before creating it
Browse files Browse the repository at this point in the history
Fixes #112
  • Loading branch information
loicmathieu committed Mar 19, 2024
1 parent fea290f commit 386fe21
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/java/io/kestra/storage/gcs/GcsStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,23 @@ public URI put(String tenantId, URI uri, InputStream data) throws IOException {
}

private void mkdirs(String path) {
path = path.replaceAll("^/*", "");
if (!path.endsWith("/")) {
path = path.substring(0, path.lastIndexOf("/") + 1);
}
System.out.println(path);

// check if it exists before creating it
Page<Blob> existing = this.storage.list(this.config.getBucket(), Storage.BlobListOption.pageSize(1), Storage.BlobListOption.prefix(path));
if(existing != null && existing.hasNextPage()) {
return;
}

String[] directories = path.split("/");
StringBuilder aggregatedPath = new StringBuilder("/");
// perform 1 put request per parent directory in the path
for (int i = 0; i <= directories.length - (path.endsWith("/") ? 1 : 2); i++) {
for (int i = 1; i < directories.length; i++) {
aggregatedPath.append(directories[i]).append("/");
System.out.println(aggregatedPath);
BlobInfo blobInfo = BlobInfo
.newBuilder(this.blob(aggregatedPath.toString()))
.build();
Expand Down

0 comments on commit 386fe21

Please sign in to comment.