[Fs] [Tencent Cos] Support writer

This commit is contained in:
qianmoQ 2024-08-23 14:56:41 +08:00
parent 7d19233fdf
commit f04a0454f3
3 changed files with 101 additions and 1 deletions

View File

@ -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

View File

@ -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)
}
}
}
}

View File

@ -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)
}
}