Skip to content

Commit

Permalink
feat: 增加了阿里云的 oss 的上传
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojinzi123 committed Apr 7, 2024
1 parent 5427655 commit 2d63f71
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dependencies {
api project(':module-bugly1')
api project(':module-ffmpeg')
api project(':module-wx-sdk')
api project(':module-ali-oss')
api project(':module-ali-pay')
api project(':module-image-crop')
api project(':module-storage')
Expand Down
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ext.versions = [
rxjava : '2.2.21',
rxandroid : "2.1.1",
component_version : "v1.0.8-rc5",
android_support_version : "v1.0-beta212",
android_support_version : "v1.0-beta218",
compose_version : "1.5.4",
compose_material_version : "1.5.4",
compose_material3_version : "1.0.1",
Expand Down
1 change: 1 addition & 0 deletions module-ali-oss/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
25 changes: 25 additions & 0 deletions module-ali-oss/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-parcelize'
id 'kotlin-kapt'
id 'com.google.devtools.ksp'
id 'maven-publish'
}

android {
namespace 'com.xiaojinzi.module.common.ali.oss'
}

apply from: project.getRootProjectPath() + "/common_module.gradle"

ksp {
arg("ModuleName", "module_common_ali_oss")
}

dependencies {
// 依赖 ali oss
implementation 'com.aliyun.dpa:oss-android-sdk:2.9.19'
}

apply from: "../common_publish.gradle"
Empty file.
21 changes: 21 additions & 0 deletions module-ali-oss/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
4 changes: 4 additions & 0 deletions module-ali-oss/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.xiaojinzi.module.common.ali.oss

import android.app.Application
import com.xiaojinzi.component.anno.ModuleAppAnno
import com.xiaojinzi.component.application.IApplicationLifecycle
import com.xiaojinzi.component.application.IModuleNotifyChanged


@ModuleAppAnno
class AliOssModuleApplication : IApplicationLifecycle, IModuleNotifyChanged {

override fun onModuleChanged(app: Application) {
}

override fun onCreate(app: Application) {
}

override fun onDestroy() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.xiaojinzi.module.common.ali.oss.spi

import com.alibaba.sdk.android.oss.ClientException
import com.alibaba.sdk.android.oss.OSSClient
import com.alibaba.sdk.android.oss.ServiceException
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider
import com.alibaba.sdk.android.oss.common.auth.OSSFederationCredentialProvider
import com.alibaba.sdk.android.oss.common.auth.OSSFederationToken
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask
import com.alibaba.sdk.android.oss.model.PutObjectRequest
import com.alibaba.sdk.android.oss.model.PutObjectResult
import com.xiaojinzi.component.anno.ServiceAnno
import com.xiaojinzi.module.common.base.spi.AliOssSpi
import com.xiaojinzi.module.common.base.spi.FileUploadServiceBaseImpl
import com.xiaojinzi.module.common.base.spi.FileUploadTaskDto
import com.xiaojinzi.support.ktx.app
import com.xiaojinzi.support.ktx.extension
import com.xiaojinzi.support.ktx.newUUid
import java.util.concurrent.ConcurrentHashMap

@ServiceAnno(
value = [AliOssSpi::class]
)
class AliOssFileUploadServiceImpl : FileUploadServiceBaseImpl(), AliOssSpi {

private val taskMap: ConcurrentHashMap<String, OSSAsyncTask<PutObjectResult>> =
ConcurrentHashMap()

private var endpoint: String = ""
private var bucket: String = ""

override suspend fun init(endpoint: String, bucket: String) {
this.endpoint = endpoint
this.bucket = bucket
}

override fun doUpload(task: FileUploadTaskDto) {

val fileKey = "${
if (task.extendPrefix.isEmpty()) {
""
} else {
"${task.extendPrefix}_"
}
}${newUUid()}.${task.targetFile.name.extension}"

val request = PutObjectRequest(
bucket,
fileKey,
task.targetFile.path,
).apply {
this.setProgressCallback { _, currentSize, totalSize ->
postProgress(
uid = task.uuid,
progress = if (totalSize <= 0) {
0f
} else {
(currentSize.toFloat() / totalSize)
},
)
}
}

val credentialProvider: OSSCredentialProvider = object : OSSFederationCredentialProvider() {
override fun getFederationToken(): OSSFederationToken {
// 从您的服务器中获取Token。
return OSSFederationToken(
"STS.NTGjwL7wHcGsy82MMq2Mb6wfL",
"CtAnW5airamRuSZErz4aa96ByXZTcE7Jf4YupSARF4Sk",
"CAISvgJ1q6Ft5B2yfSjIr5fyIc342qhp1IWYex6DqU0kPsJO2bLNrjz2IHhMeXdsAOkbsvwylWBZ6P8TlqBiStpGSAnNddMoPX+/WID4MeT7oMWQweEuqv/MQBq+aXPS2MvVfJ+KLrf0ceusbFbpjzJ6xaCAGxypQ12iN+/i6/clFKN1ODO1dj1bHtxbCxJ/ocsBTxvrOO2qLwThjxi7biMqmHIl2TovtP3mn5bNskWA1QWr8IJP+dSteKrDRtJ3IZJyX+2y2OFLbafb2EZSkUMSrPoo0PUdpGuf44DNUgcIsg/nIuuR8dAqJRdhdk2qynT/eQ2Xf5RazQ+z/+yfo34lVWrlhiB+0j9O6n9lCTHY18e4SMdq9k63pvSlHLebnf1KL/z1Gi1kFXQPhLEOJ35IaHXuToXDnvSiUkZM3vbtuMkagAEwIZysmx9gozZHC6l9JzyCN3vvRAXoQh0g5jSjRoFM3bdlv25v/f4Hh0YIEbQK946M3ruNKTTiuJnW+SdfnroLQgT19gzkX1yBDGyLGYzajBUisF3oizw4QmvTcljW14Pk8Fvzs6UuGYDs7bU8y8iidYCJjdv2nIhlSKBlQ8xJ/yAA",
"2024-04-07T10:34:49Z"
)
}
}
val ossClient = OSSClient(app, endpoint, credentialProvider)

ossClient.asyncPutObject(
request, object : OSSCompletedCallback<PutObjectRequest, PutObjectResult> {
override fun onSuccess(request: PutObjectRequest, result: PutObjectResult) {
request.progressCallback = null
}

override fun onFailure(
request: PutObjectRequest,
clientException: ClientException?,
serviceException: ServiceException?
) {
request.progressCallback = null
}
}
).apply {
taskMap[task.uuid] = this
}

}

override suspend fun cancelTasks(uidList: List<String>) {
uidList.forEach { uid ->
postCancel(uid = uid)
taskMap[uid]?.cancel()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.xiaojinzi.module.common.base.spi


interface AliOssSpi : FileUploadSpi {

companion object {

const val TAG = "AliOssSpi"

}

/**
* 初始化
*/
suspend fun init(
endpoint: String,
bucket: String,
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ sealed class FileUploadTaskExtendDto

@Keep
data class FileUploadTaskDto(
// 用于最终文件的名称前缀
val extendPrefix: String = "",
val uuid: String = newUUid(),
val targetFile: File,
val extend: FileUploadTaskExtendDto? = null,
Expand Down Expand Up @@ -374,7 +376,7 @@ abstract class FileUploadServiceBaseImpl : FileUploadSpi {
}

/**
* 执行上传
* 执行上传, 内部异步去做就好了
*/
abstract fun doUpload(task: FileUploadTaskDto)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ class TxCosFileUploadServiceImpl : FileUploadServiceBaseImpl(), TxCosSpi {
}

val cosPath =
"$subPath${newUUid()}.${task.targetFile.name.extension}" // 对象在存储桶中的位置标识符,即称对象键
"$subPath${
if(task.extendPrefix.isEmpty()) {
""
} else {
"${task.extendPrefix}_"
}
}${newUUid()}.${task.targetFile.name.extension}" // 对象在存储桶中的位置标识符,即称对象键

val srcPath: String = task.targetFile.path

Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ include ':module-image-crop'
include ':module-storage'
include ':module-bugly1'
include ':module-develop'
include ':module-ali-oss'

0 comments on commit 2d63f71

Please sign in to comment.