diff --git a/cpp/src/arrow/filesystem/gcsfs.cc b/cpp/src/arrow/filesystem/gcsfs.cc index fb77be56e7b..167526dce4e 100644 --- a/cpp/src/arrow/filesystem/gcsfs.cc +++ b/cpp/src/arrow/filesystem/gcsfs.cc @@ -182,6 +182,14 @@ class GcsFileSystem::Impl { return GetFileInfoImpl(path, std::move(meta).status(), FileType::Directory); } + Status DeleteFile(const GcsPath& p) { + if (!p.object.empty() && p.object.back() == '/') { + return Status::IOError("The given path (" + p.full_path + + ") is a directory, use DeleteDir"); + } + return internal::ToArrowStatus(client_.DeleteObject(p.bucket, p.object)); + } + Status CopyFile(const GcsPath& src, const GcsPath& dest) { auto metadata = client_.RewriteObjectBlocking(src.bucket, src.object, dest.bucket, dest.object); @@ -272,7 +280,8 @@ Status GcsFileSystem::DeleteRootDirContents() { } Status GcsFileSystem::DeleteFile(const std::string& path) { - return Status::NotImplemented("The GCS FileSystem is not fully implemented"); + ARROW_ASSIGN_OR_RAISE(auto p, GcsPath::FromString(path)); + return impl_->DeleteFile(p); } Status GcsFileSystem::Move(const std::string& src, const std::string& dest) { diff --git a/cpp/src/arrow/filesystem/gcsfs_test.cc b/cpp/src/arrow/filesystem/gcsfs_test.cc index f3e12cb5974..081cc916ed8 100644 --- a/cpp/src/arrow/filesystem/gcsfs_test.cc +++ b/cpp/src/arrow/filesystem/gcsfs_test.cc @@ -286,6 +286,23 @@ TEST_F(GcsIntegrationTest, GetFileInfoObject) { arrow::fs::AssertFileInfo(fs.get(), PreexistingObjectPath(), FileType::File); } +TEST_F(GcsIntegrationTest, DeleteFileSuccess) { + auto fs = internal::MakeGcsFileSystemForTest(TestGcsOptions()); + ASSERT_OK(fs->DeleteFile(PreexistingObjectPath())); + arrow::fs::AssertFileInfo(fs.get(), PreexistingObjectPath(), FileType::NotFound); +} + +TEST_F(GcsIntegrationTest, DeleteFileFailure) { + auto fs = internal::MakeGcsFileSystemForTest(TestGcsOptions()); + ASSERT_RAISES(IOError, fs->DeleteFile(NotFoundObjectPath())); +} + +TEST_F(GcsIntegrationTest, DeleteFileDirectoryFails) { + auto fs = internal::MakeGcsFileSystemForTest(TestGcsOptions()); + const auto path = std::string(kPreexistingBucket) + "/DeleteFileDirectoryFails/"; + ASSERT_RAISES(IOError, fs->DeleteFile(path)); +} + TEST_F(GcsIntegrationTest, CopyFileSuccess) { auto fs = internal::MakeGcsFileSystemForTest(TestGcsOptions()); const auto destination_path = kPreexistingBucket + std::string("/copy-destination");