@@ -72,7 +72,14 @@ public Blob pickABlob(String path) {
72
72
return null ;
73
73
}
74
74
GCSPath gcsPath = GCSPath .from (path );
75
- Page <Blob > blobPage = storage .list (gcsPath .getBucket (), Storage .BlobListOption .prefix (gcsPath .getName ()));
75
+ Page <Blob > blobPage ;
76
+ try {
77
+ blobPage = storage .list (gcsPath .getBucket (), Storage .BlobListOption .prefix (gcsPath .getName ()));
78
+ } catch (Exception e ) {
79
+ String errorReason = String .format ("Unable to list objects in bucket %s." , gcsPath .getBucket ());
80
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , errorReason , ErrorType .UNKNOWN ,
81
+ true , GCPUtils .GCS_SUPPORTED_DOC_URL );
82
+ }
76
83
Iterator <Blob > iterator = blobPage .getValues ().iterator ();
77
84
while (iterator .hasNext ()) {
78
85
Blob blob = iterator .next ();
@@ -93,7 +100,13 @@ public void setMetaData(Blob blob, Map<String, String> metaData) {
93
100
if (blob == null || metaData == null || metaData .isEmpty ()) {
94
101
return ;
95
102
}
96
- storage .update (BlobInfo .newBuilder (blob .getBlobId ()).setMetadata (metaData ).build ());
103
+ try {
104
+ storage .update (BlobInfo .newBuilder (blob .getBlobId ()).setMetadata (metaData ).build ());
105
+ } catch (Exception e ) {
106
+ String errorReason = String .format ("Unable to update metadata for blob %s." , blob .getName ());
107
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , errorReason , ErrorType .UNKNOWN ,
108
+ true , GCPUtils .GCS_SUPPORTED_DOC_URL );
109
+ }
97
110
}
98
111
99
112
/**
@@ -106,7 +119,14 @@ public void mapMetaDataForAllBlobs(String path, Consumer<Map<String, String>> fu
106
119
return ;
107
120
}
108
121
GCSPath gcsPath = GCSPath .from (path );
109
- Page <Blob > blobPage = storage .list (gcsPath .getBucket (), Storage .BlobListOption .prefix (gcsPath .getName ()));
122
+ Page <Blob > blobPage ;
123
+ try {
124
+ blobPage = storage .list (gcsPath .getBucket (), Storage .BlobListOption .prefix (gcsPath .getName ()));
125
+ } catch (Exception e ) {
126
+ String errorReason = String .format ("Unable to list objects in bucket %s." , gcsPath .getBucket ());
127
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , errorReason , ErrorType .UNKNOWN ,
128
+ true , GCPUtils .GCS_SUPPORTED_DOC_URL );
129
+ }
110
130
Iterator <Blob > blobIterator = blobPage .iterateAll ().iterator ();
111
131
while (blobIterator .hasNext ()) {
112
132
Blob blob = blobIterator .next ();
@@ -179,9 +199,16 @@ public void move(GCSPath sourcePath, GCSPath destPath, boolean recursive, boolea
179
199
* Get all the matching wildcard paths given the regex input.
180
200
*/
181
201
public List <GCSPath > getMatchedPaths (GCSPath sourcePath , boolean recursive , Pattern wildcardRegex ) {
182
- Page <Blob > blobPage = storage .list (sourcePath .getBucket (), Storage .BlobListOption .prefix (
202
+ Page <Blob > blobPage ;
203
+ try {
204
+ blobPage = storage .list (sourcePath .getBucket (), Storage .BlobListOption .prefix (
183
205
getWildcardPathPrefix (sourcePath , wildcardRegex )
184
- ));
206
+ ));
207
+ } catch (Exception e ) {
208
+ String errorReason = String .format ("Unable to list objects in bucket %s." , sourcePath .getBucket ());
209
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , errorReason , ErrorType .UNKNOWN ,
210
+ true , GCPUtils .GCS_SUPPORTED_DOC_URL );
211
+ }
185
212
List <String > blobPageNames = new ArrayList <>();
186
213
blobPage .getValues ().forEach (blob -> blobPageNames .add (blob .getName ()));
187
214
return getFilterMatchedPaths (sourcePath , blobPageNames , recursive );
@@ -217,8 +244,7 @@ static List<GCSPath> getFilterMatchedPaths(GCSPath sourcePath, List<String> blob
217
244
*/
218
245
private void pairTraverse (GCSPath sourcePath , GCSPath destPath , boolean recursive , boolean overwrite ,
219
246
Consumer <BlobPair > consumer ) {
220
-
221
- Bucket sourceBucket = null ;
247
+ Bucket sourceBucket ;
222
248
try {
223
249
sourceBucket = storage .get (sourcePath .getBucket ());
224
250
} catch (Exception e ) {
@@ -232,7 +258,7 @@ private void pairTraverse(GCSPath sourcePath, GCSPath destPath, boolean recursiv
232
258
throw ErrorUtils .getProgramFailureException (new ErrorCategory (ErrorCategory .ErrorCategoryEnum .PLUGIN ),
233
259
errorReason , errorReason , ErrorType .USER , true , null );
234
260
}
235
- Bucket destBucket = null ;
261
+ Bucket destBucket ;
236
262
try {
237
263
destBucket = storage .get (destPath .getBucket ());
238
264
} catch (Exception e ) {
@@ -250,24 +276,45 @@ private void pairTraverse(GCSPath sourcePath, GCSPath destPath, boolean recursiv
250
276
251
277
boolean destinationBaseExists ;
252
278
String baseDestName = destPath .getName ();
253
- if (destPath .isBucket () || storage .get (BlobId .of (destPath .getBucket (), baseDestName )) != null ) {
279
+ Blob storageBlob ;
280
+ try {
281
+ storageBlob = storage .get (BlobId .of (destPath .getBucket (), baseDestName ));
282
+ } catch (Exception e ) {
283
+ String errorReason = String .format ("Unable to access GCS object '%s'." , baseDestName );
284
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , errorReason , ErrorType .UNKNOWN ,
285
+ true , GCPUtils .GCS_SUPPORTED_DOC_URL );
286
+ }
287
+ if (destPath .isBucket () || storageBlob != null ) {
254
288
destinationBaseExists = true ;
255
289
} else {
256
290
// if gs://bucket2/subdir doesn't exist, check if gs://bucket2/subdir/ exists
257
291
// similarly, if gs://bucket2/subdir/ doesn't exist, check if gs://bucket2/subdir exists
258
292
// this is because "cp dir0 subdir" and "cp dir0 subdir/" are equivalent if the 'subdir' directory exists
259
293
String modifiedName = baseDestName .endsWith ("/" ) ?
260
294
baseDestName .substring (0 , baseDestName .length () - 1 ) : baseDestName + "/" ;
261
- destinationBaseExists = storage .get (BlobId .of (destPath .getBucket (), modifiedName )) != null ;
295
+ try {
296
+ destinationBaseExists = storage .get (BlobId .of (destPath .getBucket (), modifiedName )) != null ;
297
+ } catch (Exception e ) {
298
+ String errorReason = String .format ("Unable to access GCS object '%s'." , modifiedName );
299
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , errorReason , ErrorType .UNKNOWN ,
300
+ true , GCPUtils .GCS_SUPPORTED_DOC_URL );
301
+ }
262
302
}
263
303
264
304
List <BlobPair > copyList = new ArrayList <>();
265
305
traverse (BlobId .of (sourcePath .getBucket (), sourcePath .getName ()), recursive , sourceBlob -> {
266
306
BlobId destBlobID = resolve (sourcePath .getName (), sourceBlob .getBlobId ().getName (),
267
307
destPath , destinationBaseExists );
268
308
if (!overwrite ) {
269
- Blob destBlob = storage .get (destBlobID );
270
- // we can't just use Blob's isDirectory() because the cloud console will create a 'directory' by creating
309
+ Blob destBlob ;
310
+ try {
311
+ destBlob = storage .get (destBlobID );
312
+ } catch (Exception e ) {
313
+ String errorReason = String .format ("Unable to access GCS object '%s'." , destBlobID .getName ());
314
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , errorReason , ErrorType .UNKNOWN ,
315
+ true , GCPUtils .GCS_SUPPORTED_DOC_URL );
316
+ }
317
+ // we can't just use Blob's isDirectory() because the cloud console will create a 'directory' by creating
271
318
// a 0 size placeholder blob that ends with '/'. This placeholder blob's isDirectory() method returns false,
272
319
// but we don't want the overwrite check to fail on it. So we explicitly ignore the check for these 0 size
273
320
// placeholder blobs.
@@ -358,8 +405,15 @@ static String append(String base, String part) {
358
405
* @param consumer the blob consumer
359
406
*/
360
407
private void traverse (BlobId blobId , boolean recursive , Consumer <Blob > consumer ) {
361
- Page <Blob > blobList = storage .list (blobId .getBucket (), Storage .BlobListOption .currentDirectory (),
362
- Storage .BlobListOption .prefix (blobId .getName ()));
408
+ Page <Blob > blobList ;
409
+ try {
410
+ blobList = storage .list (blobId .getBucket (), Storage .BlobListOption .currentDirectory (),
411
+ Storage .BlobListOption .prefix (blobId .getName ()));
412
+ } catch (Exception e ) {
413
+ String errorReason = String .format ("" );
414
+ throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , errorReason , ErrorType .UNKNOWN ,
415
+ true , GCPUtils .GCS_SUPPORTED_DOC_URL );
416
+ }
363
417
for (Blob blob : blobList .iterateAll ()) {
364
418
if (!blob .isDirectory ()) {
365
419
consumer .accept (blob );
0 commit comments