Skip to content

Commit 22b5ee9

Browse files
authored
fix: use projectId from CloudStorageConfig (#429)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) Fixes #352 ☕️
1 parent 5896991 commit 22b5ee9

2 files changed

Lines changed: 116 additions & 9 deletions

File tree

java-storage-nio/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStoragePath.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,24 @@ boolean seemsLikeADirectoryAndUsePseudoDirectories(Storage storage) {
103103
if (!prefix.endsWith("/")) {
104104
prefix += "/";
105105
}
106-
Page<Blob> list =
107-
storage.list(
108-
this.bucket(),
109-
Storage.BlobListOption.prefix(prefix),
110-
// we only look at the first result, so no need for a bigger page.
111-
Storage.BlobListOption.pageSize(1),
112-
fileSystem.provider().getProject() == null
113-
? null
114-
: Storage.BlobListOption.userProject(fileSystem.provider().getProject()));
106+
String userProject = fileSystem.config().userProject();
107+
Page<Blob> list = null;
108+
if (userProject != null) {
109+
list =
110+
storage.list(
111+
this.bucket(),
112+
Storage.BlobListOption.prefix(prefix),
113+
// we only look at the first result, so no need for a bigger page.
114+
Storage.BlobListOption.pageSize(1),
115+
Storage.BlobListOption.userProject(userProject));
116+
} else {
117+
list =
118+
storage.list(
119+
this.bucket(),
120+
Storage.BlobListOption.prefix(prefix),
121+
// we only look at the first result, so no need for a bigger page.
122+
Storage.BlobListOption.pageSize(1));
123+
}
115124
for (Blob b : list.getValues()) {
116125
// if this blob starts with our prefix and then a slash, then prefix is indeed a folder!
117126
if (b.getBlobId() == null) {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.storage.contrib.nio;
18+
19+
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.times;
21+
import static org.mockito.Mockito.verify;
22+
import static org.mockito.Mockito.when;
23+
24+
import com.google.api.gax.paging.Page;
25+
import com.google.cloud.storage.Blob;
26+
import com.google.cloud.storage.BlobId;
27+
import com.google.cloud.storage.Storage;
28+
import com.google.cloud.storage.StorageOptions;
29+
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
30+
import com.google.common.collect.Lists;
31+
import java.nio.file.Files;
32+
import org.junit.Before;
33+
import org.junit.Rule;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.junit.runners.JUnit4;
37+
38+
/** Unit tests for {@code Files.isDirectory()}. */
39+
@RunWith(JUnit4.class)
40+
public class CloudStorageIsDirectoryTest {
41+
@Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
42+
43+
private StorageOptions mockOptions;
44+
private Storage mockStorage;
45+
46+
@Before
47+
public void before() {
48+
mockOptions = mock(StorageOptions.class);
49+
mockStorage = mock(Storage.class);
50+
when(mockOptions.getService()).thenReturn(mockStorage);
51+
CloudStorageFileSystemProvider.setStorageOptions(mockOptions);
52+
}
53+
54+
@Test
55+
public void testIsDirectoryNoUserProject() {
56+
CloudStorageFileSystem fs =
57+
CloudStorageFileSystem.forBucket("bucket", CloudStorageConfiguration.DEFAULT);
58+
when(mockStorage.get(BlobId.of("bucket", "test", null)))
59+
.thenThrow(new IllegalArgumentException());
60+
Page<Blob> pages = mock(Page.class);
61+
Blob blob = mock(Blob.class);
62+
when(blob.getBlobId()).thenReturn(BlobId.of("bucket", "test/hello.txt"));
63+
when(pages.getValues()).thenReturn(Lists.newArrayList(blob));
64+
when(mockStorage.list(
65+
"bucket", Storage.BlobListOption.prefix("test/"), Storage.BlobListOption.pageSize(1)))
66+
.thenReturn(pages);
67+
68+
Files.isDirectory(fs.getPath("test"));
69+
verify(mockStorage, times(1))
70+
.list("bucket", Storage.BlobListOption.prefix("test/"), Storage.BlobListOption.pageSize(1));
71+
}
72+
73+
@Test
74+
public void testIsDirectoryWithUserProject() {
75+
CloudStorageFileSystem fs =
76+
CloudStorageFileSystem.forBucket(
77+
"bucket", CloudStorageConfiguration.builder().userProject("project-id").build());
78+
when(mockStorage.get(BlobId.of("bucket", "test", null)))
79+
.thenThrow(new IllegalArgumentException());
80+
Page<Blob> pages = mock(Page.class);
81+
Blob blob = mock(Blob.class);
82+
when(blob.getBlobId()).thenReturn(BlobId.of("bucket", "test/hello.txt"));
83+
when(pages.getValues()).thenReturn(Lists.newArrayList(blob));
84+
when(mockStorage.list(
85+
"bucket",
86+
Storage.BlobListOption.prefix("test/"),
87+
Storage.BlobListOption.pageSize(1),
88+
Storage.BlobListOption.userProject("project-id")))
89+
.thenReturn(pages);
90+
Files.isDirectory(fs.getPath("test"));
91+
verify(mockStorage, times(1))
92+
.list(
93+
"bucket",
94+
Storage.BlobListOption.prefix("test/"),
95+
Storage.BlobListOption.pageSize(1),
96+
Storage.BlobListOption.userProject("project-id"));
97+
}
98+
}

0 commit comments

Comments
 (0)