[Fs] [Tencent Cos] Support reader

This commit is contained in:
qianmoQ 2024-08-23 15:04:01 +08:00
parent f04a0454f3
commit 892289b323
4 changed files with 56 additions and 4 deletions

View File

@ -41,6 +41,24 @@ class TencentCosFs : Fs
override fun reader(request: FsRequest?): FsResponse
{
TODO("Not yet implemented")
requireNotNull(request) { "request must not be null" }
log.info("TencentCos reader origin path [ {} ]", request.fileName)
val response = FsResponse.builder()
.remote(request.fileName)
.successful(true)
.build()
try
{
response.context = TencentCosUtils.reader(request)
log.info("TencentCos reader [ {} ] successfully", request.fileName)
}
catch (e: java.lang.Exception)
{
log.error("TencentCos reader error", e)
response.isSuccessful = false
response.message = e.message
}
return response
}
}

View File

@ -3,12 +3,14 @@ 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.GetObjectRequest
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
@ -24,10 +26,10 @@ class TencentCosUtils
@JvmStatic
fun copy(request: FsRequest, stream: InputStream, fileName: String): String?
{
val client = getClient(request)
try
{
val client = getClient(request)
val objectMetadata = ObjectMetadata()
objectMetadata.contentLength = stream.available().toLong()
objectMetadata.cacheControl = "no-cache"
@ -40,6 +42,31 @@ class TencentCosUtils
{
throw RuntimeException(e)
}
finally
{
client.shutdown()
}
}
@JvmStatic
fun reader(request: FsRequest): InputStream
{
val client = getClient(request)
try
{
val getObjectRequest = GetObjectRequest(request.bucket, request.fileName)
val cosObject = client.getObject(getObjectRequest)
return cosObject.objectContent
}
catch (e: Exception)
{
throw RuntimeException(e)
}
finally
{
client.shutdown()
}
}
}
}

View File

@ -75,7 +75,7 @@ class TencentCosFsTest
}
@Test
fun testDelete()
fun delete()
{
val plugins: Set<Fs?>? = injector?.getInstance(Key.get(object : TypeLiteral<Set<Fs?>?>()
{}))

View File

@ -1,6 +1,7 @@
package io.edurt.datacap.fs.cos
import io.edurt.datacap.fs.FsRequest
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
@ -27,4 +28,10 @@ class TencentCosUtilsTest
val result = TencentCosUtils.copy(request, stream, "TencentCosFsTest.kt")
assertTrue(result != null)
}
@Test
fun reader()
{
assertNotNull(TencentCosUtils.reader(request))
}
}