From bca099f47659e916d0d14cad5619dc9e9f7c4f7c Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Sun, 25 Aug 2024 19:50:11 +0800 Subject: [PATCH] [Fs] [Amazon S3] Support delete --- .../io/edurt/datacap/fs/s3/AmazonS3Fs.kt | 22 +++++++++++++++++++ .../io/edurt/datacap/fs/s3/AmazonS3Utils.kt | 20 +++++++++++++++++ .../io/edurt/datacap/fs/s3/AmazonS3FsTest.kt | 10 +++++++++ .../edurt/datacap/fs/s3/AmazonS3UtilsTest.kt | 6 +++++ 4 files changed, 58 insertions(+) diff --git a/fs/datacap-fs-amazon-s3/src/main/kotlin/io/edurt/datacap/fs/s3/AmazonS3Fs.kt b/fs/datacap-fs-amazon-s3/src/main/kotlin/io/edurt/datacap/fs/s3/AmazonS3Fs.kt index 6a30eb42..da3f881f 100644 --- a/fs/datacap-fs-amazon-s3/src/main/kotlin/io/edurt/datacap/fs/s3/AmazonS3Fs.kt +++ b/fs/datacap-fs-amazon-s3/src/main/kotlin/io/edurt/datacap/fs/s3/AmazonS3Fs.kt @@ -61,4 +61,26 @@ class AmazonS3Fs : Fs } return response } + + override fun delete(request: FsRequest?): FsResponse + { + requireNotNull(request) { "request must not be null" } + + try + { + val status = AmazonS3Utils.delete(request) + log.info("{} delete [ {} ] successfully", this.name(), request.fileName) + return FsResponse.builder() + .successful(status) + .build() + } + catch (e: java.lang.Exception) + { + log.error("{} delete error", this.name(), e) + return FsResponse.builder() + .successful(false) + .message(e.message) + .build() + } + } } diff --git a/fs/datacap-fs-amazon-s3/src/main/kotlin/io/edurt/datacap/fs/s3/AmazonS3Utils.kt b/fs/datacap-fs-amazon-s3/src/main/kotlin/io/edurt/datacap/fs/s3/AmazonS3Utils.kt index fb7c9f72..3a7670e7 100644 --- a/fs/datacap-fs-amazon-s3/src/main/kotlin/io/edurt/datacap/fs/s3/AmazonS3Utils.kt +++ b/fs/datacap-fs-amazon-s3/src/main/kotlin/io/edurt/datacap/fs/s3/AmazonS3Utils.kt @@ -122,5 +122,25 @@ class AmazonS3Utils throw RuntimeException(e) } } + + @JvmStatic + fun delete(request: FsRequest): Boolean + { + val client = getClient(request) + + try + { + client.deleteObject(request.bucket, request.fileName) + return true + } + catch (e: Exception) + { + throw RuntimeException(e) + } + finally + { + client.shutdown() + } + } } } diff --git a/fs/datacap-fs-amazon-s3/src/test/kotlin/io/edurt/datacap/fs/s3/AmazonS3FsTest.kt b/fs/datacap-fs-amazon-s3/src/test/kotlin/io/edurt/datacap/fs/s3/AmazonS3FsTest.kt index 6dd4f0ff..59faf53e 100644 --- a/fs/datacap-fs-amazon-s3/src/test/kotlin/io/edurt/datacap/fs/s3/AmazonS3FsTest.kt +++ b/fs/datacap-fs-amazon-s3/src/test/kotlin/io/edurt/datacap/fs/s3/AmazonS3FsTest.kt @@ -73,4 +73,14 @@ class AmazonS3FsTest log.error("Reader error", e) } } + + @Test + fun delete() + { + val plugins: Set? = injector?.getInstance(Key.get(object : TypeLiteral?>() + {})) + val plugin: Fs? = plugins?.first { v -> v?.name().equals(name) } + val response = plugin !!.delete(request) + assertTrue(response.isSuccessful) + } } diff --git a/fs/datacap-fs-amazon-s3/src/test/kotlin/io/edurt/datacap/fs/s3/AmazonS3UtilsTest.kt b/fs/datacap-fs-amazon-s3/src/test/kotlin/io/edurt/datacap/fs/s3/AmazonS3UtilsTest.kt index 6317d3bf..ae835e4e 100644 --- a/fs/datacap-fs-amazon-s3/src/test/kotlin/io/edurt/datacap/fs/s3/AmazonS3UtilsTest.kt +++ b/fs/datacap-fs-amazon-s3/src/test/kotlin/io/edurt/datacap/fs/s3/AmazonS3UtilsTest.kt @@ -34,4 +34,10 @@ class AmazonS3UtilsTest { assertNotNull(AmazonS3Utils.reader(request)) } + + @Test + fun delete() + { + assertTrue(AmazonS3Utils.delete(request)) + } }