Android中阿里云OSS文件上传的方法?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!
一、项目导入阿里云oss的SDK
方式1:在build文件添加依赖(博主和官方一致推荐)
dependencies { //阿里云oss implementation 'com.aliyun.dpa:oss-android-sdk:+' }
方式2:源码编译jar包 源码传送门:官方阿里云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文件上传的方法大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。