From 2e95b563cdd880c8e863d3562f33788d9fc735e0 Mon Sep 17 00:00:00 2001 From: Andrew Sung Date: Fri, 10 Jul 2020 17:01:37 -0400 Subject: [PATCH] Fixes to delete a file only if it exists. --- .../java/gyro/google/GoogleStorageFileBackend.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/gyro/google/GoogleStorageFileBackend.java b/src/main/java/gyro/google/GoogleStorageFileBackend.java index af9ae7e9..52882a89 100644 --- a/src/main/java/gyro/google/GoogleStorageFileBackend.java +++ b/src/main/java/gyro/google/GoogleStorageFileBackend.java @@ -21,12 +21,11 @@ import java.io.OutputStream; import java.util.stream.Stream; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.ByteArrayContent; import com.google.api.services.storage.Storage; import com.google.api.services.storage.model.StorageObject; - import com.psddev.dari.util.ObjectUtils; - import gyro.core.FileBackend; import gyro.core.GyroException; import gyro.core.Type; @@ -94,7 +93,14 @@ public void close() { @Override public void delete(String file) throws Exception { - client().objects().delete(getBucket(), prefixed(file)).execute(); + // Delete if exists. + try { + client().objects().delete(getBucket(), prefixed(file)).execute(); + } catch (GoogleJsonResponseException e) { + if (e.getStatusCode() != 404) { + throw e; + } + } } private Storage client() {