支持以下类型:

  1. String(filepath): /storage/emulated/0/Pictures/Screenshots/Screenshot_20201017_085301_com.ss.android.ugc.aweme.jpg
  2. Uri(fileUri): content://media/external/file/404557
  1. implementation 'me.luzhuo.android:lib_cos:1.0.1-SNAPSHOT'
  2. implementation 'me.luzhuo.android:lib_okhttp:1.0.7-SNAPSHOT'
  3. implementation 'com.squareup.okhttp3:okhttp:3.14.9'

1. 使用

1. 上传单个文件

  1. COSUtils.getInstance(this).uploadFileByTencentCOS(COSFileBean(filePath.absolutePath, HashManager.getInstance().uuid), { progress, currentSize, totalSize -> Log.e(TAG, "progress: $progress - $currentSize - $totalSize"); }, object : ICOSFileCallback {
  2. override fun onSuccess(fileBean: COSFileBean?) {
  3. Log.e(TAG, "success: $fileBean");
  4. }
  5. override fun onError(errorMessage: String?) {
  6. Log.e(TAG, "error: $errorMessage")
  7. }
  8. })

2. 上传多个文件

  1. val files = arrayListOf<COSFileBean>().apply {
  2. repeat(10) {
  3. add(COSFileBean(Uri.parse("content://media/external/file/404487"), HashManager.getInstance().uuid))
  4. }
  5. }
  6. COSUtils.getInstance(this).uploadFilesByTencentCOS(files, { progress, currentSize, totalSize -> Log.e(TAG, "progress: $progress - $currentSize - $totalSize"); }, object : ICOSFilesCallback {
  7. override fun onSuccess(fileBeans: MutableList<COSFileBean>?) {
  8. fileBeans?.forEach {
  9. Log.e(TAG, "success: $it");
  10. }
  11. }
  12. override fun onError(errorMessage: String?) {
  13. Log.e(TAG, "error: $errorMessage")
  14. }
  15. })

2. 工具类

COSUtils.java

  1. /* Copyright 2021 Luzhuo. All rights reserved.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package me.luzhuo.tencentcosdemo;
  16. import android.content.Context;
  17. import java.util.List;
  18. import me.luzhuo.lib_cos.ITencentCOSFileServer;
  19. import me.luzhuo.lib_cos.TencentCOSFileServer;
  20. import me.luzhuo.lib_cos.bean.COSCAMBean;
  21. import me.luzhuo.lib_cos.bean.COSFileBean;
  22. import me.luzhuo.lib_cos.callback.ICOSFileCallback;
  23. import me.luzhuo.lib_cos.callback.ICOSFilesCallback;
  24. import me.luzhuo.lib_cos.callback.IProgress;
  25. import me.luzhuo.lib_okhttp.IOKHttpManager;
  26. import me.luzhuo.lib_okhttp.OKHttpManager;
  27. import me.luzhuo.lib_okhttp.interceptor.TokenInterceptor;
  28. /**
  29. * Tencent COS 文件上传工具类
  30. */
  31. public class COSUtils {
  32. private static COSUtils instance;
  33. private ITencentCOSFileServer tencentCOSFileServer;
  34. private IOKHttpManager okHttpManager;
  35. private static final String CAMServer = "http://api-chongjia.jincaishuizu.com/find/app/v1/pic/getAliCloudSupposeAcc";
  36. private static final String region = "ap-shanghai"; // 存储桶所在的地域
  37. private static final String bucketName = "cos-demo-1257757876"; // 桶名
  38. private static final String cosPath = "temp/cos/"; // 对象存储在桶中的位置 temp/cos/
  39. public static COSUtils getInstance(Context context) {
  40. if (instance == null){
  41. synchronized (COSUtils.class){
  42. if (instance == null){
  43. instance = new COSUtils(context);
  44. }
  45. }
  46. }
  47. return instance;
  48. }
  49. private COSUtils(Context context) {
  50. try {
  51. okHttpManager = new OKHttpManager(
  52. new TokenInterceptor() {
  53. @Override
  54. public String getToken() {
  55. return "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzMzE2MyIsImlhdCI6MTYwNzc2NjMwMSwiZXhwIjoxNjA4MzcxMTAxfQ.P1-KMLC4xFhjQng9RxiujRazubv6SlxdU3KRT6OhcRrRGE7w4q2yR7VFANGkj7EieBUNXpZP_Pnf861UVPOH3w";
  56. }
  57. });
  58. }catch (Exception e){
  59. e.printStackTrace();
  60. }
  61. tencentCOSFileServer = new TencentCOSFileServer(context, region, bucketName, cosPath) {
  62. @Override
  63. protected COSCAMBean getTokenFromUser() {
  64. try {
  65. // String message = okHttpManager.get(CAMServer);
  66. // TODO 解析json数据
  67. return new COSCAMBean("AKIDAyZc6RNbsmXL5Y7Zlkmrtkz6wCAhBxDSGtdn6LMWX4ikT88tRjVa1xwYUJoczDj1", "RC7njcpsZLfg0dQjYED+pO3lCi66FzfJ8jLvc3CEqAs=", "HLoIHzVg4NMb7vUXyWjcWQGOt0sWQ7Ha907d83486d0ce048fc7b29e9593f0585eCL1BL2lKQcGb4klykck2geyRNhTTkmEJ1pIC0YrASUyD0pKuHuMQNYDc-lDACc03Iu3OOEJyYlC-4RTzYUVRo2y2AR3IRSQ069C7wD-0AvrD0vxtWpBAIYKwcVt50Im99qmIE7rgs4qepgpGdwJfYMejc3oVgIYx-SKuaKTzvfcGl6_K7l0VKBt24LNg5jNrJIs98Rkqxz_abNKIGpTA3H1B2RGO4YyLY91AAUsdrJ-6RvV-50GgkFqJbSDcQuMKZ0o12HoUETLwwoX1zBPqtbcPibTEV3oXCZAgf-vJah0Ls7KGofYRdVeABj8k4XeW7OgIQSeuwJeVFtIZMGxMIodYRMI3tl-H1CMzDkd3mdXRc0VFbaoZ0tg5cTJSIDsCbGs9HiZt0NcGSB9fJUDhoFdN23rKPibh5t0ZJvk3bMzl9s87b5l8MVxRlD0MiN_oWU0796K9hGSZW0OpKNbA5ZC8p1FC5UDPGFP_ooRfYtQW0x3omN1mYA2Ab1fvuYH", 1636734436);
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. return null;
  72. }
  73. };
  74. }
  75. public void uploadFileByTencentCOS(final COSFileBean fileBean, final IProgress progress, final ICOSFileCallback callback) {
  76. tencentCOSFileServer.uploadFileByTencentCOS(fileBean, progress, callback);
  77. }
  78. public void uploadFilesByTencentCOS(final List<COSFileBean> fileBeans, final IProgress progress, final ICOSFilesCallback callback) {
  79. tencentCOSFileServer.uploadFilesByTencentCOS(fileBeans, progress, callback);
  80. }
  81. }

3. 混淆

  1. # cosxml
  2. -dontwarn com.tencent.cos.**
  3. -keep class com.tencent.cos.xml.**{*;}
  4. # foundation
  5. -keep class com.tencent.qcloud.cos.**{*;}
  6. # beacon
  7. -dontwarn com.tencent.beacon.**
  8. -keep class com.tencent.beacon.** { *; }
  9. -dontwarn com.tencent.qmsp.**
  10. -keep class com.tencent.qmsp.** { *; }