From f04a0454f3b9ca25520a4a2b4c9f8549421e2e33 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Fri, 23 Aug 2024 14:56:41 +0800 Subject: [PATCH] [Fs] [Tencent Cos] Support writer --- .../io/edurt/datacap/fs/cos/TencentCosFs.kt | 27 ++++++++++- .../edurt/datacap/fs/cos/TencentCosUtils.kt | 45 +++++++++++++++++++ .../datacap/fs/cos/TencentCosUtilsTest.kt | 30 +++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 fs/datacap-fs-tencent-cos/src/main/kotlin/io/edurt/datacap/fs/cos/TencentCosUtils.kt create mode 100644 fs/datacap-fs-tencent-cos/src/test/kotlin/io/edurt/datacap/fs/cos/TencentCosUtilsTest.kt diff --git a/fs/datacap-fs-tencent-cos/src/main/kotlin/io/edurt/datacap/fs/cos/TencentCosFs.kt b/fs/datacap-fs-tencent-cos/src/main/kotlin/io/edurt/datacap/fs/cos/TencentCosFs.kt index d91aaeb3..875e4ebd 100644 --- a/fs/datacap-fs-tencent-cos/src/main/kotlin/io/edurt/datacap/fs/cos/TencentCosFs.kt +++ b/fs/datacap-fs-tencent-cos/src/main/kotlin/io/edurt/datacap/fs/cos/TencentCosFs.kt @@ -3,7 +3,10 @@ package io.edurt.datacap.fs.cos import io.edurt.datacap.fs.Fs import io.edurt.datacap.fs.FsRequest import io.edurt.datacap.fs.FsResponse +import io.edurt.datacap.fs.cos.TencentCosUtils.Companion.copy import org.slf4j.LoggerFactory.getLogger +import java.io.File +import java.lang.String.join class TencentCosFs : Fs { @@ -11,7 +14,29 @@ class TencentCosFs : Fs override fun writer(request: FsRequest?): FsResponse { - TODO("Not yet implemented") + requireNotNull(request) { "request must not be null" } + + log.info("TencentCos writer origin path [ {} ]", request.fileName) + val targetPath = join(File.separator, request.endpoint, request.bucket, request.fileName) + val response = FsResponse.builder() + .origin(request.fileName) + .remote(targetPath) + .successful(true) + .build() + log.info("TencentCos writer target path [ {} ]", request.fileName) + try + { + val key = copy(request, request.stream, request.fileName) + response.remote = key + log.info("TencentCos writer [ {} ] successfully", key) + } + catch (e: Exception) + { + log.error("TencentCos writer error", e) + response.isSuccessful = false + response.message = e.message + } + return response } override fun reader(request: FsRequest?): FsResponse diff --git a/fs/datacap-fs-tencent-cos/src/main/kotlin/io/edurt/datacap/fs/cos/TencentCosUtils.kt b/fs/datacap-fs-tencent-cos/src/main/kotlin/io/edurt/datacap/fs/cos/TencentCosUtils.kt new file mode 100644 index 00000000..bc3f9bbc --- /dev/null +++ b/fs/datacap-fs-tencent-cos/src/main/kotlin/io/edurt/datacap/fs/cos/TencentCosUtils.kt @@ -0,0 +1,45 @@ +package io.edurt.datacap.fs.cos + +import com.qcloud.cos.COSClient +import com.qcloud.cos.ClientConfig +import com.qcloud.cos.auth.BasicCOSCredentials +import com.qcloud.cos.model.ObjectMetadata +import com.qcloud.cos.model.PutObjectRequest +import com.qcloud.cos.region.Region +import io.edurt.datacap.fs.FsRequest +import java.io.InputStream + +class TencentCosUtils +{ + companion object + { + private fun getClient(request: FsRequest): COSClient + { + val cred = BasicCOSCredentials(request.access, request.secret) + val region: Region = Region(request.endpoint) + val clientConfig = ClientConfig(region) + return COSClient(cred, clientConfig) + } + + @JvmStatic + fun copy(request: FsRequest, stream: InputStream, fileName: String): String? + { + try + { + val client = getClient(request) + + val objectMetadata = ObjectMetadata() + objectMetadata.contentLength = stream.available().toLong() + objectMetadata.cacheControl = "no-cache" + val putObject = PutObjectRequest(request.bucket, request.fileName, stream, objectMetadata) + + val response = client.putObject(putObject) + return response.requestId + } + catch (e: Exception) + { + throw RuntimeException(e) + } + } + } +} diff --git a/fs/datacap-fs-tencent-cos/src/test/kotlin/io/edurt/datacap/fs/cos/TencentCosUtilsTest.kt b/fs/datacap-fs-tencent-cos/src/test/kotlin/io/edurt/datacap/fs/cos/TencentCosUtilsTest.kt new file mode 100644 index 00000000..2fb00226 --- /dev/null +++ b/fs/datacap-fs-tencent-cos/src/test/kotlin/io/edurt/datacap/fs/cos/TencentCosUtilsTest.kt @@ -0,0 +1,30 @@ +package io.edurt.datacap.fs.cos + +import io.edurt.datacap.fs.FsRequest +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import java.io.FileInputStream + +class TencentCosUtilsTest +{ + private var request = FsRequest() + + @Before + fun before() + { + request.access = System.getProperty("access") + request.secret = System.getProperty("secret") + request.bucket = System.getProperty("bucket") + request.fileName = "TencentCosFsTest.kt" + request.endpoint = System.getProperty("endpoint") + } + + @Test + fun copy() + { + val stream = FileInputStream("src/test/kotlin/io/edurt/datacap/fs/cos/TencentCosFsTest.kt") + val result = TencentCosUtils.copy(request, stream, "TencentCosFsTest.kt") + assertTrue(result != null) + } +}