Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

FileExists check if a file exists

Iakov Davydov7ae708082450
diff --git a/fs/fs.go b/fs/fs.go
index e339ed222..a96bff041 100644
--- a/fs/fs.go
+++ b/fs/fs.go
@@ -733,3 +733,16 @@ func CheckClose(c io.Closer, err *error) {
*err = cerr
}
}
+
+// FileExists returns true if a file remote exists.
+// If remote is a directory, FileExists returns false.
+func FileExists(fs Fs, remote string) (bool, error) {
+ _, err := fs.NewObject(remote)
+ if err != nil {
+ if err == ErrorObjectNotFound || err == ErrorNotAFile {
+ return false, nil
+ }
+ return false, err
+ }
+ return true, nil
+}