Commit 36fe67f8 authored by yinjiacheng's avatar yinjiacheng

add 金山云存储获取token

parent 93484557
package com.yidian.shenghuoquan.newscontent.bean
/**
* author: yinjiacheng
* date: 5/20/21 8:03 PM
* description: 金山云token 与AppServer约定的RSA加密原始数据格式
*/
data class KSYunTokenBean(
val secret: String?,
val http_method: String,
val date: String,
val resource: String,
val content_type: String,
val content_md5: String,
val headers: String
)
...@@ -2,6 +2,7 @@ package com.yidian.shenghuoquan.newscontent.utils ...@@ -2,6 +2,7 @@ package com.yidian.shenghuoquan.newscontent.utils
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import com.google.gson.Gson
import com.ksyun.ks3.exception.Ks3Error import com.ksyun.ks3.exception.Ks3Error
import com.ksyun.ks3.model.acl.CannedAccessControlList import com.ksyun.ks3.model.acl.CannedAccessControlList
import com.ksyun.ks3.services.AuthListener import com.ksyun.ks3.services.AuthListener
...@@ -10,7 +11,12 @@ import com.ksyun.ks3.services.Ks3ClientConfiguration ...@@ -10,7 +11,12 @@ import com.ksyun.ks3.services.Ks3ClientConfiguration
import com.ksyun.ks3.services.handler.PutObjectACLResponseHandler import com.ksyun.ks3.services.handler.PutObjectACLResponseHandler
import com.ksyun.ks3.services.handler.PutObjectResponseHandler import com.ksyun.ks3.services.handler.PutObjectResponseHandler
import com.ksyun.ks3.services.request.PutObjectRequest import com.ksyun.ks3.services.request.PutObjectRequest
import com.yidian.common.utils.EncryptUtil import com.yidian.framework.mobile.xdiamond.SecretUtil
import com.yidian.shenghuoquan.newscontent.bean.KSYunTokenBean
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.httpbean.GetKSYunTokenBean
import com.yidian.utils.MD5Util
import com.yidian.yac.ftdevicefinger.core.FtDeviceFingerManager
import cz.msebera.android.httpclient.Header import cz.msebera.android.httpclient.Header
import java.io.File import java.io.File
...@@ -24,12 +30,16 @@ class Ks3Core private constructor() : AuthListener { ...@@ -24,12 +30,16 @@ class Ks3Core private constructor() : AuthListener {
private lateinit var client: Ks3Client private lateinit var client: Ks3Client
private lateinit var putObjectResponseHandler: PutObjectResponseHandler private lateinit var putObjectResponseHandler: PutObjectResponseHandler
private lateinit var putObjectACLResponseHandler: PutObjectACLResponseHandler private lateinit var putObjectACLResponseHandler: PutObjectACLResponseHandler
private lateinit var json: Gson
private val onKs3UploadListenerList: ArrayList<OnKs3UploadListener> by lazy { ArrayList<OnKs3UploadListener>() } private val onKs3UploadListenerList: ArrayList<OnKs3UploadListener> by lazy { ArrayList<OnKs3UploadListener>() }
companion object { companion object {
const val TAG = "Ks3Core" const val TAG = "Ks3Core"
const val BUCKET_NAME = "bp-yidian" const val BUCKET_NAME = "bp-yidian"
const val END_POINT = "ks3-cn-beijing.ksyun.com" const val END_POINT = "ks3-cn-beijing.ksyun.com"
const val VIDEO_SUFFIX = ".mp4"
const val IMAGE_SUFFIX = ".png"
const val KSYUN_TOKEN_APPID = "merchant-b"
val instance: Ks3Core by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { val instance: Ks3Core by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
Ks3Core() Ks3Core()
} }
...@@ -41,8 +51,12 @@ class Ks3Core private constructor() : AuthListener { ...@@ -41,8 +51,12 @@ class Ks3Core private constructor() : AuthListener {
fun init(context: Context) { fun init(context: Context) {
initClient(context) initClient(context)
initListener() initListener()
initOther()
} }
/**
* 初始化金山云存储client
*/
private fun initClient(context: Context) { private fun initClient(context: Context) {
client = Ks3Client(this, context) client = Ks3Client(this, context)
client.setConfiguration(Ks3ClientConfiguration.getDefaultConfiguration()) client.setConfiguration(Ks3ClientConfiguration.getDefaultConfiguration())
...@@ -67,7 +81,10 @@ class Ks3Core private constructor() : AuthListener { ...@@ -67,7 +81,10 @@ class Ks3Core private constructor() : AuthListener {
response: String?, response: String?,
throwable: Throwable? throwable: Throwable?
) { ) {
Log.e(TAG, "put object fail:, errorMsg: ${error?.errorMessage}") Log.e(
TAG,
"put object fail:, stateCode: $statesCode, errorMsg: ${error?.errorMessage}, response: $response"
)
if (onKs3UploadListenerList.size > 0) { if (onKs3UploadListenerList.size > 0) {
for (listener in onKs3UploadListenerList) { for (listener in onKs3UploadListenerList) {
listener.onUploadFailure(error?.errorMessage) listener.onUploadFailure(error?.errorMessage)
...@@ -128,57 +145,59 @@ class Ks3Core private constructor() : AuthListener { ...@@ -128,57 +145,59 @@ class Ks3Core private constructor() : AuthListener {
} }
} }
/** private fun initOther() {
* 文件上传 json = Gson()
* @param filePath 本地文件全路径 }
*/
/*fun uploadFile(filePath: String) {
client.putObject(
BUCKET_NAME,
EncryptUtil.getMD5(filePath),
File(filePath),
putObjectResponseHandler
)
}*/
/** /**
* 文件上传 * 文件上传
* @param filePath 本地文件全路径 * @param path 本地文件全路径
*/ */
fun uploadFile(filePath: String) { fun uploadObject(path: String, type: ObjectType) {
val request = PutObjectRequest(BUCKET_NAME, EncryptUtil.getMD5(filePath), File(filePath)) val file = File(path)
request.cannedAcl = CannedAccessControlList.PublicRead val request = PutObjectRequest(BUCKET_NAME, getObjectKey(file.name, type), file)
request.cannedAcl = CannedAccessControlList.Private
client.putObject(request, putObjectResponseHandler) client.putObject(request, putObjectResponseHandler)
} }
/** /**
* 为已存在于bucket中的object设置访问控制权限 * 获取ObjectKey
* 作为Object在云空间的唯一标识
*/ */
private fun putObjectACL(filePath: String) { private fun getObjectKey(name: String, type: ObjectType): String {
val controlList = CannedAccessControlList.PublicRead return MD5Util.md5Encrypt32Upper(name + FtDeviceFingerManager.getDeviceFinger()) +
client.putObjectACL( if (type == ObjectType.VIDEO) VIDEO_SUFFIX else IMAGE_SUFFIX
BUCKET_NAME,
EncryptUtil.getMD5(filePath),
controlList,
putObjectACLResponseHandler
)
} }
override fun onCalculateAuth( override fun onCalculateAuth(
httpMethod: String?, httpMethod: String,
ContentType: String?, contentType: String,
Date: String?, date: String,
ContentMD5: String?, contentMD5: String,
Resource: String?, resource: String,
Headers: String? headers: String
): String { ): String? {
// 此处由APP端向业务服务器发送post请求返回Token // 此处由APP端向业务服务器发送post请求返回Token
// 需要注意该回调方法运行在非主线程 // 该回调方法运行在非主线程
Log.d( Log.d(
TAG, TAG,
"httpMethod: $httpMethod, ContentType: $ContentType, Date: $Date, ContentMD5: $ContentMD5, Resource: $Resource, Headers: $Headers" "KSYun calculate auth, httpMethod: $httpMethod, contentType: $contentType, date: $date, contentMD5: $contentMD5, resource: $resource, headers: $headers"
) )
return "" val tok = SecretUtil.rsaEncrypt(
json.toJson(
KSYunTokenBean(
SecretUtil.sign(httpMethod + date + resource),
httpMethod,
date,
resource,
contentType,
contentMD5,
headers
)
)
)
return ApiService.getKSYunToken(GetKSYunTokenBean.Request(KSYUN_TOKEN_APPID, tok))
?.result?.token
} }
fun addOnKs3UploadListener(listener: OnKs3UploadListener) { fun addOnKs3UploadListener(listener: OnKs3UploadListener) {
...@@ -191,6 +210,9 @@ class Ks3Core private constructor() : AuthListener { ...@@ -191,6 +210,9 @@ class Ks3Core private constructor() : AuthListener {
onKs3UploadListenerList.remove(listener) onKs3UploadListenerList.remove(listener)
} }
/**
* 业务监听
*/
interface OnKs3UploadListener { interface OnKs3UploadListener {
fun onUploadStart() fun onUploadStart()
fun onUploadProgress(progress: Double) fun onUploadProgress(progress: Double)
...@@ -200,4 +222,11 @@ class Ks3Core private constructor() : AuthListener { ...@@ -200,4 +222,11 @@ class Ks3Core private constructor() : AuthListener {
fun onUploadFailure(message: String?) fun onUploadFailure(message: String?)
} }
/**
* 上传对象的类型
*/
enum class ObjectType {
VIDEO, IMAGE
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment