估计有小伙伴看到阿里云oss的api文档十脸懵逼了,啊哈哈哈,接下来博主就来拯救你了,看完觉得好的就关注博主吧,么么么么么哒!!!
想要自己研究阿里云oss的,博主开启传送门给你:阿里云oss
一、项目导入阿里云oss的SDK
方式1:在build文件添加依赖(博主和官方一致推荐)
dependencies { //阿里云oss implementation 'com.aliyun.dpa:oss-android-sdk:+' }
方式2:源码编译jar包 源码传送门:官方阿里云oss源码
二、OSS所需要的Android权限
三、在项目的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() { @Override public void onProgress(PutObjectRequest request, long currentSize, long totalSize) { } }); OSSAsyncTask ossAsyncTask = oss.asyncPutObject(putObjectRequest, new OSSCompletedCallback () { @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 () { @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文件上传内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!