支持以下类型:
- String(filepath): /storage/emulated/0/Pictures/Screenshots/Screenshot_20201017_085301_com.ss.android.ugc.aweme.jpg
- Uri(fileUri): content://media/external/file/404557
implementation 'me.luzhuo.android:lib_cos:1.0.1-SNAPSHOT'
implementation 'me.luzhuo.android:lib_okhttp:1.0.7-SNAPSHOT'
implementation 'com.squareup.okhttp3:okhttp:3.14.9'
1. 使用
1. 上传单个文件
COSUtils.getInstance(this).uploadFileByTencentCOS(COSFileBean(filePath.absolutePath, HashManager.getInstance().uuid), { progress, currentSize, totalSize -> Log.e(TAG, "progress: $progress - $currentSize - $totalSize"); }, object : ICOSFileCallback {
override fun onSuccess(fileBean: COSFileBean?) {
Log.e(TAG, "success: $fileBean");
}
override fun onError(errorMessage: String?) {
Log.e(TAG, "error: $errorMessage")
}
})
2. 上传多个文件
val files = arrayListOf<COSFileBean>().apply {
repeat(10) {
add(COSFileBean(Uri.parse("content://media/external/file/404487"), HashManager.getInstance().uuid))
}
}
COSUtils.getInstance(this).uploadFilesByTencentCOS(files, { progress, currentSize, totalSize -> Log.e(TAG, "progress: $progress - $currentSize - $totalSize"); }, object : ICOSFilesCallback {
override fun onSuccess(fileBeans: MutableList<COSFileBean>?) {
fileBeans?.forEach {
Log.e(TAG, "success: $it");
}
}
override fun onError(errorMessage: String?) {
Log.e(TAG, "error: $errorMessage")
}
})
2. 工具类
/* Copyright 2021 Luzhuo. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.luzhuo.tencentcosdemo;
import android.content.Context;
import java.util.List;
import me.luzhuo.lib_cos.ITencentCOSFileServer;
import me.luzhuo.lib_cos.TencentCOSFileServer;
import me.luzhuo.lib_cos.bean.COSCAMBean;
import me.luzhuo.lib_cos.bean.COSFileBean;
import me.luzhuo.lib_cos.callback.ICOSFileCallback;
import me.luzhuo.lib_cos.callback.ICOSFilesCallback;
import me.luzhuo.lib_cos.callback.IProgress;
import me.luzhuo.lib_okhttp.IOKHttpManager;
import me.luzhuo.lib_okhttp.OKHttpManager;
import me.luzhuo.lib_okhttp.interceptor.TokenInterceptor;
/**
* Tencent COS 文件上传工具类
*/
public class COSUtils {
private static COSUtils instance;
private ITencentCOSFileServer tencentCOSFileServer;
private IOKHttpManager okHttpManager;
private static final String CAMServer = "http://api-chongjia.jincaishuizu.com/find/app/v1/pic/getAliCloudSupposeAcc";
private static final String region = "ap-shanghai"; // 存储桶所在的地域
private static final String bucketName = "cos-demo-1257757876"; // 桶名
private static final String cosPath = "temp/cos/"; // 对象存储在桶中的位置 temp/cos/
public static COSUtils getInstance(Context context) {
if (instance == null){
synchronized (COSUtils.class){
if (instance == null){
instance = new COSUtils(context);
}
}
}
return instance;
}
private COSUtils(Context context) {
try {
okHttpManager = new OKHttpManager(
new TokenInterceptor() {
@Override
public String getToken() {
return "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzMzE2MyIsImlhdCI6MTYwNzc2NjMwMSwiZXhwIjoxNjA4MzcxMTAxfQ.P1-KMLC4xFhjQng9RxiujRazubv6SlxdU3KRT6OhcRrRGE7w4q2yR7VFANGkj7EieBUNXpZP_Pnf861UVPOH3w";
}
});
}catch (Exception e){
e.printStackTrace();
}
tencentCOSFileServer = new TencentCOSFileServer(context, region, bucketName, cosPath) {
@Override
protected COSCAMBean getTokenFromUser() {
try {
// String message = okHttpManager.get(CAMServer);
// TODO 解析json数据
return new COSCAMBean("AKIDAyZc6RNbsmXL5Y7Zlkmrtkz6wCAhBxDSGtdn6LMWX4ikT88tRjVa1xwYUJoczDj1", "RC7njcpsZLfg0dQjYED+pO3lCi66FzfJ8jLvc3CEqAs=", "HLoIHzVg4NMb7vUXyWjcWQGOt0sWQ7Ha907d83486d0ce048fc7b29e9593f0585eCL1BL2lKQcGb4klykck2geyRNhTTkmEJ1pIC0YrASUyD0pKuHuMQNYDc-lDACc03Iu3OOEJyYlC-4RTzYUVRo2y2AR3IRSQ069C7wD-0AvrD0vxtWpBAIYKwcVt50Im99qmIE7rgs4qepgpGdwJfYMejc3oVgIYx-SKuaKTzvfcGl6_K7l0VKBt24LNg5jNrJIs98Rkqxz_abNKIGpTA3H1B2RGO4YyLY91AAUsdrJ-6RvV-50GgkFqJbSDcQuMKZ0o12HoUETLwwoX1zBPqtbcPibTEV3oXCZAgf-vJah0Ls7KGofYRdVeABj8k4XeW7OgIQSeuwJeVFtIZMGxMIodYRMI3tl-H1CMzDkd3mdXRc0VFbaoZ0tg5cTJSIDsCbGs9HiZt0NcGSB9fJUDhoFdN23rKPibh5t0ZJvk3bMzl9s87b5l8MVxRlD0MiN_oWU0796K9hGSZW0OpKNbA5ZC8p1FC5UDPGFP_ooRfYtQW0x3omN1mYA2Ab1fvuYH", 1636734436);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
};
}
public void uploadFileByTencentCOS(final COSFileBean fileBean, final IProgress progress, final ICOSFileCallback callback) {
tencentCOSFileServer.uploadFileByTencentCOS(fileBean, progress, callback);
}
public void uploadFilesByTencentCOS(final List<COSFileBean> fileBeans, final IProgress progress, final ICOSFilesCallback callback) {
tencentCOSFileServer.uploadFilesByTencentCOS(fileBeans, progress, callback);
}
}
3. 混淆
# cosxml
-dontwarn com.tencent.cos.**
-keep class com.tencent.cos.xml.**{*;}
# foundation
-keep class com.tencent.qcloud.cos.**{*;}
# beacon
-dontwarn com.tencent.beacon.**
-keep class com.tencent.beacon.** { *; }
-dontwarn com.tencent.qmsp.**
-keep class com.tencent.qmsp.** { *; }