All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.edurt.datacap.fs.cos.TencentCosUtils.kt Maven / Gradle / Ivy

There is a newer version: 2024.03.9
Show newest version
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
    {
        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?
        {
            val client = getClient(request)

            try
            {
                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)
            }
            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()
            }
        }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy