From ce340ccc128a59974ef06d7fd224f49c79f7a4a5 Mon Sep 17 00:00:00 2001 From: Christoph Stepper Date: Tue, 14 Sep 2021 08:01:46 +0200 Subject: [PATCH] save_object: wrap s3HTTP call in try to be able to remove files created for missing objects. --- R/get_object.R | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/R/get_object.R b/R/get_object.R index b15b79c..8e352f3 100644 --- a/R/get_object.R +++ b/R/get_object.R @@ -118,13 +118,22 @@ function(object, } # use httr::write_disk() to write directly to disk - r <- s3HTTP(verb = "GET", - bucket = bucket, - path = paste0("/", object), - headers = headers, - write_disk = httr::write_disk(path = file, overwrite = overwrite), - ...) - return(file) + r <- try( + s3HTTP(verb = "GET", + bucket = bucket, + path = paste0("/", object), + headers = headers, + write_disk = httr::write_disk(path = file, overwrite = overwrite), + ...) + ) + + if (inherits(r, "try-error")) { + file.remove(file) + if (length(dir(d)) == 0) file.remove(d) + stop(r, call. = FALSE) + } + + return(file) } #' @rdname get_object