mirror of
https://gitee.com/devlive-community/datacap.git
synced 2024-12-02 03:57:35 +08:00
[Fs] [Tencent Cos] Support reader
This commit is contained in:
parent
f04a0454f3
commit
892289b323
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ class TencentCosFsTest
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete()
|
||||
fun delete()
|
||||
{
|
||||
val plugins: Set<Fs?>? = injector?.getInstance(Key.get(object : TypeLiteral<Set<Fs?>?>()
|
||||
{}))
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user