Android端使用阿里云OSS上传文件的正确姿势
前言:OSS是Object Storage Server的全称,意思是对象存储服务,对象存储服务(Object Storage Service,OSS)是一种海量、安全、低成本、高可靠的云存储服务,适合存放任意类型的文件。容量和处理能力弹性扩展,多种存储类型供选择,全面优化存储成本。想了解更多OSS,可以查看官方文档的介绍,对于OSS的集成,官网也给出了详细的文档,我这里只对OSS在Android端的使用做一个简单的介绍,毕竟对于从未使用过OSS的Android开发者来说,第一次使用还是会遇到一些坑,希望我的这篇文章能帮助到一些同行。
一、整备工作
第一步:你得有一个阿里云账号,也可以选择其他方式进行登录,这里我选择的是钉钉登录,登录方式自由选择【阿里云】
第二步:进入到OSS控制台,创建Bucket,用于存储图片或者其他文件的桶,类似于一个文件仓库,我这里创建的是image-header,用来专门存放头像图片的文件仓库,命名规则在创建时会提示你
第三步:设置Bucket的访问权限,选择【访问控制】,进入到【RAM访问控制】页面,设置想要问的访问权限,这里对权限的设置不做具体的介绍,官方有相应的文档介绍
二、在Android Studio中集成
第一步:在项目的app的build.gradle中添加依赖
implementation 'com.aliyun.dpa:oss-android-sdk:2.5.0'
第二步:通过OSS相关的方法上传文件
// 第一个参数:在【RAM访问控制】创建用户时分配的accessKeyId
// 第二个参数:在【RAM访问控制】创建用户时分配的accessKeySecret
OSSCredentialProvider ossCredentialProvider = new OSSPlainTextAKSKCredentialProvider("LT7SHiiyuJd8saHJsdscjkesd8dekkh", "aPKvkjshkSHuw43Lkswd8JKjdsklajaJlWc");
// 第一个参数:上下文
// 第二个参数:在OSS控制台创建好Bucket后,会有一个EndPoint(地域节点),比如我这里的节点是:http://oss-cn-shanghai.aliyuncs.com
// 第三个参数:OSSCredentialProvider
OSS oss = new OSSClient(mContext.getApplicationContext(), “http://oss-cn-shanghai.aliyuncs.com”, ossCredentialProvider);
// 第二个参数:可以是一个文件路径:比如你在image-header创建了一个文件夹为images,那第二个参数传images/img.jpg
// 第三个参数:是文件的本地路径,比如我这里在本地路径中是姨丈图片,路径是sdcard/img/img.jpg
PutObjectRequest put = new PutObjectRequest("image-header", "img.jpg", "sdcard/img/img.jpg");
// 异步上传时可以设置进度回调。
put.setProgressCallback((request, currentSize, totalSize) -> {
Log.d("currentSize = " + currentSize, "totalSize = " + totalSize);
});
// 此处调用异步上传方法
OSSAsyncTask ossAsyncTask= oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
@Override
public void onSuccess(PutObjectRequest request, PutObjectResult result) {
Log.d("PutObject", "UploadSuccess");
Log.d("ETag", result.getETag());
Log.d("RequestId", result.getRequestId());
}
@Override
public void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException) {
// 请求异常。
if (clientException != null) {
// 本地异常,如网络异常等。
Log.e(clientException.getMessage());
}
if (serviceException != null) {
// 服务异常。
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
- 注意:以上参数都要填写正确,否则会上传失败,如果提示访问被拒绝,一般都是因为你设置的权限不够,需要检测你的权限是否给足。
三、 以下是我自己使用OSS封装的一个类,仅供参考
public class OssManager {
/**
* Context
*/
private Context mContext;
/**
* bucket name
*/
private String mBucketName;
/**
* access key id
*/
private String mAccessKeyId;
/**
* access key secret
*/
private String mAccessKeySecret;
/**
* end point url
*/
private String mEndPoint;
/**
* file name or file dir
*/
private String mObjectKey;
/**
* local file path
*/
private String mLocalFilePath;
/**
* push file progress listener
*/
private OnPushProgressListener onPushProgressListener;
/**
* push file state
*/
private OnPushStateListener onPushStateListener;
/**
* OSS async task
*/
private OSSAsyncTask mOSSAsyncTask;
private OssManager(Context context, String bucketName, String accessKeyId, String accessKeySecret, String endPoint, String objectKey, String localFilePath) {
this.mContext = context;
this.mBucketName = bucketName;
this.mAccessKeyId = accessKeyId;
this.mAccessKeySecret = accessKeySecret;
this.mEndPoint = endPoint;
this.mObjectKey = objectKey;
this.mLocalFilePath = localFilePath;
}
/**
* set push file progress listener,pushing call back onProgress(PutObjectRequest request, long currentSize, long totalSize)
*
* @param listener push file progress listener
*/
public void setPushProgressListener(OnPushProgressListener listener) {
this.onPushProgressListener = listener;
}
/**
* set push file state listener,push success call back onSuccess(PutObjectRequest request, PutObjectResult result)
* push failed call back onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException)
*
* @param listener push file state listener
*/
public void setPushStateListener(OnPushStateListener listener) {
this.onPushStateListener = listener;
}
/**
* push file to oss,this method is async task
*/
public void push() {
OSSCredentialProvider ossCredentialProvider = new OSSPlainTextAKSKCredentialProvider(mAccessKeyId, mAccessKeySecret);
OSS oss = new OSSClient(mContext.getApplicationContext(), mEndPoint, ossCredentialProvider);
onPush(oss);
}
/**
* push file to oss,this method is async task
*/
public void push(String accessKeyId, String accessKeySecret, String securityToken) {
if (accessKeyId == null || accessKeySecret == null || securityToken == null) return;
OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider(accessKeyId, accessKeySecret, securityToken);
OSS oss = new OSSClient(mContext.getApplicationContext(), mEndPoint, credentialProvider);
onPush(oss);
}
/**
* push
* @param oss OSS
*/
private void onPush(OSS oss) {
PutObjectRequest put = new PutObjectRequest(mBucketName, mObjectKey, mLocalFilePath);
// 异步上传时可以设置进度回调。
put.setProgressCallback((request, currentSize, totalSize) -> {
LogUtils.d("currentSize = " + currentSize, "totalSize = " + totalSize);
if (onPushProgressListener != null) {
onPushProgressListener.onProgress(request, currentSize, totalSize);
}
});
mOSSAsyncTask = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
@Override
public void onSuccess(PutObjectRequest request, PutObjectResult result) {
LogUtils.d("PutObject", "UploadSuccess");
LogUtils.d("ETag", result.getETag());
LogUtils.d("RequestId", result.getRequestId());
if (onPushStateListener != null) {
onPushStateListener.onSuccess(request, result);
}
}
@Override
public void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException) {
// 请求异常。
if (clientException != null) {
// 本地异常,如网络异常等。
LogUtils.e(clientException.getMessage());
}
if (serviceException != null) {
// 服务异常。
LogUtils.e("ErrorCode", serviceException.getErrorCode());
LogUtils.e("RequestId", serviceException.getRequestId());
LogUtils.e("HostId", serviceException.getHostId());
LogUtils.e("RawMessage", serviceException.getRawMessage());
}
if (onPushStateListener != null) {
onPushStateListener.onFailure(request, clientException, serviceException);
}
}
});
}
/**
* cancel file push task
*/
public void cancelPush() {
if (mOSSAsyncTask != null && !mOSSAsyncTask.isCanceled() && !mOSSAsyncTask.isCompleted()) {
mOSSAsyncTask.cancel();
}
}
/**
* OssManager builder,init params
*/
public static class Builder {
private Context context;
private String bucketName;
private String accessKeyId;
private String accessKeySecret;
private String endPoint;
private String objectKey;
private String localFilePath;
public Builder(Context context) {
this.context = context;
}
public Builder bucketName(String bucketName) {
this.bucketName = bucketName;
return this;
}
public Builder accessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
return this;
}
public Builder accessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
return this;
}
public Builder endPoint(String endPint) {
this.endPoint = endPint;
return this;
}
public Builder objectKey(String objectKey) {
this.objectKey = objectKey;
return this;
}
public Builder localFilePath(String localFilePath) {
this.localFilePath = localFilePath;
return this;
}
public OssManager build() {
return new OssManager(context, bucketName, accessKeyId, accessKeySecret, endPoint, objectKey, localFilePath);
}
}
public interface OnPushProgressListener {
void onProgress(PutObjectRequest request, long currentSize, long totalSize);
}
public interface OnPushStateListener {
void onSuccess(PutObjectRequest request, PutObjectResult result);
void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException);
}
}
结语:OSS的使用我就分享到这里,第一次使用OSS时,看了官网给的介绍,没怎么看懂,踩了一些坑,虽然花了不少心思,最后还是成功把文件上传至阿里云OSS,更多关于OSS的介绍,建议还是看官网给的介绍。