diff --git a/src/main/java/io/kestra/storage/gcs/GcsStorage.java b/src/main/java/io/kestra/storage/gcs/GcsStorage.java index fdc14ce..5113582 100644 --- a/src/main/java/io/kestra/storage/gcs/GcsStorage.java +++ b/src/main/java/io/kestra/storage/gcs/GcsStorage.java @@ -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 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 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();