欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 阿里云OSS文件上传的实现示例

程序员文章站 2024-01-09 19:17:58
估计有小伙伴看到阿里云oss的api文档十脸懵逼了,啊哈哈哈,接下来博主就来拯救你了,看完觉得好的就关注博主吧,么么么么么哒!!!想要自己研究阿里云oss的,博主开启传送门给你:阿里云oss一、项目导...

估计有小伙伴看到阿里云oss的api文档十脸懵逼了,啊哈哈哈,接下来博主就来拯救你了,看完觉得好的就关注博主吧,么么么么么哒!!!

想要自己研究阿里云oss的,博主开启传送门给你:阿里云oss

一、项目导入阿里云oss的sdk

方式1:在build文件添加依赖(博主和官方一致推荐)

dependencies {
	 //阿里云oss
  implementation 'com.aliyun.dpa:oss-android-sdk:+'
}

方式2:源码编译jar包 源码传送门:官方阿里云oss源码

Android 阿里云OSS文件上传的实现示例

二、oss所需要的android权限

<uses-permission android:name="android.permission.internet" />
<uses-permission android:name="android.permission.access_network_state" />
<uses-permission android:name="android.permission.access_wifi_state" />
<uses-permission android:name="android.permission.write_external_storage" />
<uses-permission android:name="android.permission.mount_unmount_filesystems" />

三、在项目的proguard-rules.pro文件加入混淆配置

-keep class com.alibaba.sdk.android.oss.** { *; }
-dontwarn okio.**
-dontwarn org.apache.commons.codec.binary.**

四、重点来了,上代码(只有上传和删除文件的,需要其他的功能就需要自己去研究了)

import android.content.context;
import android.util.log;

import com.alibaba.sdk.android.oss.clientconfiguration;
import com.alibaba.sdk.android.oss.clientexception;
import com.alibaba.sdk.android.oss.oss;
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.callback.ossprogresscallback;
import com.alibaba.sdk.android.oss.common.auth.osscredentialprovider;
import com.alibaba.sdk.android.oss.common.auth.ossststokencredentialprovider;
import com.alibaba.sdk.android.oss.internal.ossasynctask;
import com.alibaba.sdk.android.oss.model.deleteobjectrequest;
import com.alibaba.sdk.android.oss.model.deleteobjectresult;
import com.alibaba.sdk.android.oss.model.putobjectrequest;
import com.alibaba.sdk.android.oss.model.putobjectresult;

public class aliyunuploadfile {
  private aliyunuploadview aliyunuploadview;
  private osscredentialprovider credentialprovider;
  private clientconfiguration conf;
  private oss oss;

  public aliyunuploadfile(aliyunuploadview aliyunuploadview) {
    this.aliyunuploadview = aliyunuploadview;
  }

/**
   *
   * @param context 上下文
   * @param accesskeyid 存取键 一般找你们后端拿
   * @param secretkeyid 密钥 一般找你们后端拿
   * @param securitytoken 安全符记 一般找你们后端拿
   * @param endpoint 端点 一般找你们后端拿
   * @param bucketname 桶名 一般找你们后端拿
   * @param objectname 文件名
   * @param uploadfilepath 文件路径
   */
  public void uploadfile(context context, string accesskeyid, string secretkeyid, string securitytoken
      , string endpoint, string bucketname, string objectname, string uploadfilepath) {

    credentialprovider = new ossststokencredentialprovider(accesskeyid, secretkeyid, securitytoken);
    conf = new clientconfiguration();
    conf.setconnectiontimeout(15 * 1000); // 连接超时,默认15秒
    conf.setsockettimeout(15 * 1000); // socket超时,默认15秒
    conf.setmaxconcurrentrequest(5); // 最大并发请求数,默认5个
    conf.setmaxerrorretry(2); // 失败后最大重试次数,默认2次
    oss = new ossclient(context, endpoint, credentialprovider, conf);

    // 构造上传请求。
    putobjectrequest putobjectrequest = new putobjectrequest(bucketname, objectname, uploadfilepath);

    // 异步上传时可以设置进度回调。
    putobjectrequest.setprogresscallback(new ossprogresscallback<putobjectrequest>() {
      @override
      public void onprogress(putobjectrequest request, long currentsize, long totalsize) {

      }
    });

    ossasynctask ossasynctask = oss.asyncputobject(putobjectrequest, new osscompletedcallback<putobjectrequest, putobjectresult>() {
      @override
      public void onsuccess(putobjectrequest request, putobjectresult result) {
        aliyunuploadview.uploadsuccess(oss.presignpublicobjecturl(bucketname,objectname));
      }

      @override
      public void onfailure(putobjectrequest request, clientexception clientexcepion, serviceexception serviceexception) {
        // 请求异常。
        if (clientexcepion != null) {
          // 本地异常,如网络异常等。
          clientexcepion.printstacktrace();
          log.e("123",clientexcepion+"");
          aliyunuploadview.uploaddefeated("网络异常");
        }

        if (serviceexception != null) {
          // 服务异常。
          log.e("123",serviceexception+"");
          aliyunuploadview.uploaddefeated("服务异常");
        }
      }
    });

    // ossasynctask.cancel(); // 可以取消任务
    // ossasynctask.waituntilfinished(); // 等待任务完成
  }

  public void deletefile(context context, string accesskeyid, string secretkeyid, string securitytoken
      , string endpoint, string bucketname, string objectname) {

    credentialprovider = new ossststokencredentialprovider(accesskeyid, secretkeyid, securitytoken);
    conf = new clientconfiguration();
    conf.setconnectiontimeout(15 * 1000); // 连接超时,默认15秒
    conf.setsockettimeout(15 * 1000); // socket超时,默认15秒
    conf.setmaxconcurrentrequest(5); // 最大并发请求数,默认5个
    conf.setmaxerrorretry(2); // 失败后最大重试次数,默认2次
    oss = new ossclient(context, endpoint, credentialprovider, conf);

    // 创建删除请求。
    deleteobjectrequest delete = new deleteobjectrequest(bucketname, objectname);
    // 异步删除。
    ossasynctask deletetask = oss.asyncdeleteobject(delete, new osscompletedcallback<deleteobjectrequest, deleteobjectresult>() {
      @override
      public void onsuccess(deleteobjectrequest request, deleteobjectresult result) {

      }

      @override
      public void onfailure(deleteobjectrequest request, clientexception clientexcepion, serviceexception serviceexception) {
        // 请求异常。
        if (clientexcepion != null) {
          // 本地异常,如网络异常等。
          clientexcepion.printstacktrace();

        }
        if (serviceexception != null) {
          // 服务异常。

        }
      }
    });

    // deletetask.cancel(); // 可以取消任务
    // deletetask.waituntilfinished(); // 等待任务完成
  }
  
	public interface aliyunuploadview {
  	void uploadsuccess(string url);

  	void uploaddefeated(string error);
	}
}

到此这篇关于android 阿里云oss文件上传的实现示例的文章就介绍到这了,更多相关android 阿里云oss文件上传内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!