@@ -126,9 +126,7 @@ public static GoogleCloudStorageClient.Builder builder(
126126 .resolveValueIfEnabled (config .getApiKey (), String .class )
127127 .orElse (null ));
128128
129- builder .useDefaultCredentialsChain (environment
130- .resolveValueIfEnabled (config .getUseDefaultCredentialsChain (), Boolean .class )
131- .orElse (false ));
129+ builder .useDefaultCredentialsChain (config .getUseDefaultCredentialsChain ());
132130
133131 return builder ;
134132 }
@@ -187,37 +185,43 @@ public Builder useDefaultCredentialsChain(boolean useDefaultCredentialsChain) {
187185 }
188186
189187 public GoogleCloudStorageClient build () throws org .geowebcache .storage .StorageException {
190- StorageOptions .Builder builder = StorageOptions .getDefaultInstance ().toBuilder ();
188+ try {
189+ StorageOptions .Builder builder = StorageOptions .getDefaultInstance ().toBuilder ();
191190
192- if (projectId != null ) {
193- builder .setProjectId (projectId );
194- }
195- if (quotaProjectId != null ) {
196- builder .setQuotaProjectId (quotaProjectId );
197- }
198- if (endpointUrl != null ) {
199- // Set custom endpoint for emulators or non-standard GCS endpoints
200- builder .setHost (endpointUrl );
201- }
202- Credentials credentials = null ;
203- if (apiKey != null ) {
204- credentials = ApiKeyCredentials .create (apiKey );
205- } else if (useDefaultCredentialsChain ) {
206- try {
207- credentials = GoogleCredentials .getApplicationDefault ();
208- } catch (IOException e ) {
209- throw new org .geowebcache .storage .StorageException ("Error obtaining default credentials" , e );
191+ if (projectId != null ) {
192+ builder .setProjectId (projectId );
210193 }
211- }
212- if (credentials != null ) {
213- // credentials need to be set after projectId and quotaProjectId so its setter will
214- // check whether projectId is null and get it from credentials if its a ServiceAccountCredentials
215- // or quotaProjectId is null and get it from credentials if it's a QuotaProjectIdProvider
216- builder .setCredentials (credentials );
217- }
218- Storage storageClient = builder .build ().getService ();
194+ if (quotaProjectId != null ) {
195+ builder .setQuotaProjectId (quotaProjectId );
196+ }
197+ if (endpointUrl != null ) {
198+ // Set custom endpoint for emulators or non-standard GCS endpoints
199+ builder .setHost (endpointUrl );
200+ }
201+ Credentials credentials = null ;
202+ if (apiKey != null ) {
203+ credentials = ApiKeyCredentials .create (apiKey );
204+ } else if (useDefaultCredentialsChain ) {
205+ try {
206+ credentials = GoogleCredentials .getApplicationDefault ();
207+ } catch (IOException e ) {
208+ throw new org .geowebcache .storage .StorageException (
209+ "Error obtaining default credentials: " + e .getMessage (), e );
210+ }
211+ }
212+ if (credentials != null ) {
213+ // credentials need to be set after projectId and quotaProjectId so its setter will
214+ // check whether projectId is null and get it from credentials if its a ServiceAccountCredentials
215+ // or quotaProjectId is null and get it from credentials if it's a QuotaProjectIdProvider
216+ builder .setCredentials (credentials );
217+ }
218+ Storage storageClient = builder .build ().getService ();
219219
220- return new GoogleCloudStorageClient (storageClient , bucket , prefix );
220+ return new GoogleCloudStorageClient (storageClient , bucket , prefix );
221+ } catch (StorageException gcse ) {
222+ throw new org .geowebcache .storage .StorageException (
223+ "Error creating GCS client: " + gcse .getMessage (), gcse );
224+ }
221225 }
222226 }
223227
@@ -265,9 +269,13 @@ public boolean blobExists(String key) throws org.geowebcache.storage.StorageExce
265269 * @param prefix The prefix to filter blobs by.
266270 * @return A stream of matching {@link Blob} objects.
267271 */
268- public Stream <Blob > list (String prefix ) {
269- return storage .list (bucket , BlobListOption .prefix (requireNonNull (prefix )))
270- .streamAll ();
272+ public Stream <Blob > list (String prefix ) throws org .geowebcache .storage .StorageException {
273+ try {
274+ return storage .list (bucket , BlobListOption .prefix (requireNonNull (prefix )))
275+ .streamAll ();
276+ } catch (StorageException gcse ) {
277+ throw new org .geowebcache .storage .StorageException (gcse .getMessage (), gcse );
278+ }
271279 }
272280
273281 /**
@@ -279,10 +287,15 @@ public Stream<Blob> list(String prefix) {
279287 * @param path The directory path to check.
280288 * @return {@code true} if at least one blob exists with this path prefix, {@code false} otherwise.
281289 */
282- public boolean directoryExists (final String path ) {
290+ public boolean directoryExists (final String path ) throws org . geowebcache . storage . StorageException {
283291 requireNonNull (path );
284292 String dirPrefix = dirPrefix (path );
285- Page <Blob > blobs = storage .list (bucket , BlobListOption .prefix (dirPrefix ), BlobListOption .pageSize (1 ));
293+ Page <Blob > blobs ;
294+ try {
295+ blobs = storage .list (bucket , BlobListOption .prefix (dirPrefix ), BlobListOption .pageSize (1 ));
296+ } catch (StorageException gcse ) {
297+ throw new org .geowebcache .storage .StorageException (gcse .getMessage (), gcse );
298+ }
286299 Iterator <Blob > iterator = blobs .getValues ().iterator ();
287300 boolean hasNext = iterator .hasNext ();
288301 if (hasNext ) {
@@ -307,7 +320,7 @@ public boolean directoryExists(final String path) {
307320 * @return {@code true} if the directory existed and the delete task was submitted, {@code false} if the directory
308321 * did not exist.
309322 */
310- public boolean deleteDirectory (String path ) {
323+ public boolean deleteDirectory (String path ) throws org . geowebcache . storage . StorageException {
311324 requireNonNull (path );
312325 if (directoryExists (path )) {
313326 String dirPrefix = dirPrefix (path );
0 commit comments