Skip to content

Commit

Permalink
fix: proper url when tenant is null (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin authored Oct 12, 2023
1 parent 561e351 commit 2be9c15
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/main/java/io/kestra/storage/gcs/GcsStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ private Storage client() {
}

private BlobId blob(String tenantId, URI uri) {
return BlobId.of(this.config.getBucket(), tenantId + uri.getPath());
String path;
if (tenantId != null) {
path = tenantId + uri.getPath();
} else {
path = uri.getPath().substring(1);
}
return BlobId.of(this.config.getBucket(), path);
}

@Override
Expand Down Expand Up @@ -135,9 +141,16 @@ public List<URI> deleteByPrefix(String tenantId, URI storagePrefix) throws IOExc
StorageBatch batch = this.client().batch();
Map<URI, StorageBatchResult<Boolean>> results = new HashMap<>();

String prefix;
if (tenantId != null) {
prefix = tenantId + storagePrefix.getPath();
} else {
prefix = storagePrefix.getPath().substring(1);
}

Page<Blob> blobs = this.client()
.list(this.config.getBucket(),
Storage.BlobListOption.prefix(tenantId + storagePrefix.getPath())
Storage.BlobListOption.prefix(prefix)
);

for (Blob blob : blobs.iterateAll()) {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/io/kestra/storage/gcs/GcsStorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ void get() throws Exception {
String prefix = IdUtils.create();
String tenantId = IdUtils.create();

get(tenantId, prefix);
}

@Test
void getNoTenant() throws Exception {
String prefix = IdUtils.create();
String tenantId = null;

get(tenantId, prefix);
}

private void get(String tenantId, String prefix) throws Exception {
URL resource = GcsStorageTest.class.getClassLoader().getResource("application.yml");
String content = CharStreams.toString(new InputStreamReader(new FileInputStream(Objects.requireNonNull(resource).getFile())));

Expand Down Expand Up @@ -104,6 +116,18 @@ void deleteByPrefix() throws Exception {
String prefix = IdUtils.create();
String tenantId = IdUtils.create();

deleteByPrefix(prefix, tenantId);
}

@Test
void deleteByPrefixNoTenant() throws Exception {
String prefix = IdUtils.create();
String tenantId = IdUtils.create();

deleteByPrefix(prefix, tenantId);
}

private void deleteByPrefix(String prefix, String tenantId) throws Exception {
URL resource = GcsStorageTest.class.getClassLoader().getResource("application.yml");

List<String> path = Arrays.asList(
Expand Down

0 comments on commit 2be9c15

Please sign in to comment.