cloudyr / googleCloudStorageR

Google Cloud Storage API to R
https://code.markedmondson.me/googleCloudStorageR
Other
104 stars 29 forks source link

How do I check if a file in a bucket exists #162

Closed lappazos closed 2 years ago

lappazos commented 2 years ago

How do I check if a file in a bucket exists?

MarkEdmondson1234 commented 2 years ago

You can use gcs_get_object() with meta=TRUE to check just the headers.

gcs_get_object("not-there", meta = TRUE)
#ℹ 2022-06-23 18:37:24 > Request Status Code:  404
#Error in `abort_http()`:
#! http_404 No such object: mark-edmondson-public-files/not-there

It creates a custom error http_404 which you can catch if you want your code to carry on, similar to:

tryCatch(gcs_get_object("not-there", meta = TRUE), http_404 = function(e) FALSE)
# [1] FALSE