File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed
java-storage-nio/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -518,16 +518,32 @@ public boolean deleteIfExists(Path path) throws IOException {
518518 throw new CloudStoragePseudoDirectoryException (cloudPath );
519519 }
520520
521+ BlobId idWithGeneration = cloudPath .getBlobId ();
522+ if (idWithGeneration .getGeneration () == null ) {
523+ Storage .BlobGetOption [] options = new BlobGetOption [0 ];
524+ if (!isNullOrEmpty (userProject )) {
525+ options = new BlobGetOption [] {Storage .BlobGetOption .userProject (userProject )};
526+ }
527+ Blob blob = storage .get (idWithGeneration , options );
528+ if (blob == null ) {
529+ // not found
530+ return false ;
531+ }
532+ idWithGeneration = blob .getBlobId ();
533+ }
534+
521535 final CloudStorageRetryHandler retryHandler =
522536 new CloudStorageRetryHandler (cloudPath .getFileSystem ().config ());
523537 // Loop will terminate via an exception if all retries are exhausted
524538 while (true ) {
525539 try {
526540 if (isNullOrEmpty (userProject )) {
527- return storage .delete (cloudPath . getBlobId ());
541+ return storage .delete (idWithGeneration , Storage . BlobSourceOption . generationMatch ());
528542 } else {
529543 return storage .delete (
530- cloudPath .getBlobId (), Storage .BlobSourceOption .userProject (userProject ));
544+ idWithGeneration ,
545+ Storage .BlobSourceOption .generationMatch (),
546+ Storage .BlobSourceOption .userProject (userProject ));
531547 }
532548 } catch (StorageException exs ) {
533549 // Will rethrow a StorageException if all retries/reopens are exhausted
You can’t perform that action at this time.
0 commit comments