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 Apr 22, 2024
1 parent 1777d26 commit a20cd71
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 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,26 @@ 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);
}

// check if it exists before creating it
Page<Blob> pathBlob = this.storage.list(this.config.getBucket(), Storage.BlobListOption.prefix(path), Storage.BlobListOption.pageSize(1));
if(pathBlob != null && pathBlob.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("/");
// check if it exists before creating it
Page<Blob> currentDir = this.storage.list(this.config.getBucket(), Storage.BlobListOption.prefix(aggregatedPath.toString()), Storage.BlobListOption.pageSize(1));
if(currentDir != null && currentDir.hasNextPage()) {
continue;
}
BlobInfo blobInfo = BlobInfo
.newBuilder(this.blob(aggregatedPath.toString()))
.build();
Expand Down

0 comments on commit a20cd71

Please sign in to comment.