前言
前一阵项目中的上传图片改为上传到阿里上,记录一下实现的过程,方便以后查看。
参考资料:官方文档
配置
Android studio添加依赖
dependencies {
compile 'com.aliyun.dpa:oss-android-sdk:2.4.5'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okio:okio:1.9.0'
}
直接引入jar包(对Android studio 或者 Eclipse 都适用)
1.在官网下载 sdk 包
2.解压后得到 jar 包,目前包括 aliyun-oss-sdk-android-x.x.x.jar、okhttp-3.x.x.jar 和 okio-1.x.x.jar
3.将以上 3 个 jar 包导入 libs 目录
权限设置
确保AndroidManifest.xml 文件中已经配置了这些权限,否则,SDK 将无法正常工作。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
混淆设置
-keep class com.alibaba.sdk.android.oss.** { *; }
-dontwarn okio.**
-dontwarn org.apache.commons.codec.binary.**
实现过程
首先为了安全起见,采用的是STS鉴权模式,则要用到的数据都是从后台获得然后应用到前台的。
1.创建OSSClient (自己在这里命名为OssService)
OSSClient为OSS 服务的 Android 客户端,它为调用者提供了一系列的方法,可以用来操作,管理存储空间(bucket)和文件(object)等。
public class OssService {
private OSS oss;
private String bucket;
private picResultCallback callback;//回调接口
private String path=地址(后台告诉);
public OssService(OSS oss, String bucket,picResultCallback callback) {
this.oss = oss;
this.bucket = bucket;
this.callback=callback;
}
/**object字段为图片的上传地址(具体地址的前缀后端给,这个是拼起
*来的一个路径)
*localFile图片的本地地址
*mProgress 进度条
*img 显示图片的控件
*type 类型
*/
public void asyncPutImage(String object, final String localFile, final ProgressBar mProgress, final ImageView img,String type) {
if (object.equals("")) {
Log.w("AsyncPutImage", "ObjectNull");
return;
}
File file = new File(localFile);
if (!file.exists()) {
Log.w("AsyncPutImage", "FileNotExist");
Log.w("LocalFile", localFile);
return;
}
// 构造上传请求
PutObjectRequest put = new PutObjectRequest(bucket, object, localFile);
put.setCallbackParam(new HashMap<String, String>() {
{
put("callbackUrl", path);
put("callbackBody", "filename=${object}&size=${size}&id=${x:id}&action=${x:action}");
//https://help.aliyun.com/document_detail/31989.html?spm=5176.doc31984.6.883.brskVg
}
});
HashMap<String, String> hashMap=new HashMap<>();
hashMap.put("x:id",id);
hashMap.put("x:action",type);
put.setCallbackVars(hashMap);
// 异步上传时可以设置进度回调
put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
@Override
public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
int progress = (int) (100 * currentSize / totalSize);
mProgress.setProgress(progress);
}
});
OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
@Override
public void onSuccess(PutObjectRequest request, final PutObjectResult result) {
Observable.just(result).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<PutObjectResult>() {
@Override
public void call(PutObjectResult putObjectResult) {
mProgress.setVisibility(View.GONE);
img.setColorFilter(null);
callback.getPicData(result,localFile);
}
});
}
@Override
public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
String info = "";
// 请求异常
if (clientExcepion != null) {
// 本地异常如网络异常等
clientExcepion.printStackTrace();
info = clientExcepion.toString();
}
if (serviceException != null) {
// 服务异常
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
info = serviceException.toString();
}
}
});
}
//成功的回调接口
public interface picResultCallback {
void getPicData(PutObjectResult data,String oldPath);
}
}
2.实现OssService的方法(在activity中)
public OssService initOSS(String endpoint, String bucket) {
OSSCredentialProvider credentialProvider;
credentialProvider = new STSGetter(tokenBean);
//设置网络参数
ClientConfiguration conf = new ClientConfiguration();
conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒
conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒
conf.setMaxConcurrentRequest(5); // 最大并发请求书,默认5个
conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次
OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider, conf);
return new OssService(oss, bucket, this);
}
3.重载OSSFederationCredentialProvider生成自己的获取STS的功能(一般自动获得token写在这里,在getFederationToken()方法中,告诉它你获得token的规则即可)
1>官方demo代码(自动更新token)
public class OSSAuthCredentialsProvider extends OSSFederationCredentialProvider {
private String mAuthServerUrl;
private AuthDecoder mDecoder;
public OSSAuthCredentialsProvider(String authServerUrl) {
this.mAuthServerUrl = authServerUrl;
}
/**
* set auth server url
* @param authServerUrl
*/
public void setAuthServerUrl(String authServerUrl) {
this.mAuthServerUrl = authServerUrl;
}
/**
* set response data decoder
* @param decoder
*/
public void setDecoder(AuthDecoder decoder) {
this.mDecoder = decoder;
}
@Override
public OSSFederationToken getFederationToken() throws ClientException {
OSSFederationToken authToken;
String authData;
try {
URL stsUrl = new URL(mAuthServerUrl);
HttpURLConnection conn = (HttpURLConnection) stsUrl.openConnection();
conn.setConnectTimeout(10000);
InputStream input = conn.getInputStream();
authData = IOUtils.readStreamAsString(input, OSSConstants.DEFAULT_CHARSET_NAME);
if (mDecoder != null) {
authData = mDecoder.decode(authData);
}
JSONObject jsonObj = new JSONObject(authData);
int statusCode = jsonObj.getInt("StatusCode");
if (statusCode == 200) {
String ak = jsonObj.getString("AccessKeyId");
String sk = jsonObj.getString("AccessKeySecret");
String token = jsonObj.getString("SecurityToken");
String expiration = jsonObj.getString("Expiration");
authToken = new OSSFederationToken(ak, sk, token, expiration);
} else {
String errorCode = jsonObj.getString("ErrorCode");
String errorMessage = jsonObj.getString("ErrorMessage");
throw new ClientException("ErrorCode: " + errorCode + "| ErrorMessage: " + errorMessage);
}
return authToken;
} catch (Exception e) {
throw new ClientException(e);
}
}
public interface AuthDecoder {
String decode(String data);
}
}
2>自己的代码(因为自己的所有数据都是从后台获得的,而且结合rxjava没有想到可以返回数据的方式,所以采用手动更新token的方式)
手动更新token的具体操作:
首先token的值存在MyApp中,第一次在进入需要用到token界面时候,先获得token的值更新MyApp中的值并记录当下的时间,如果下次再次进入任何一个需要用到token的界面的时候,则判断时间是否过期,过期则重新请求token更新token的值。
public class STSGetter extends OSSFederationCredentialProvider {
private OSSFederationToken ossFederationToken;
String ak;
String sk;
String token ;
String expiration ;
public STSGetter(TokenBean bean) {
this.ak = bean.getCredentials().getAccessKeyId();
this.sk = bean.getCredentials().getAccessKeySecret();
this.token = bean.getCredentials().getSecurityToken();
this.expiration = bean.getCredentials().getExpiration();
}
public OSSFederationToken getFederationToken() {
return new OSSFederationToken(ak,sk,token,expiration);
}
}
4.实例化OSSClient,调用上传图片方法
//实例化OSSClient (自己是在onCreate()中实例化的,当然考虑到token的过期问题,也有在onResume()中再次实例化一次)
ossService = initOSS(tokenBean.getBucket().getEndPoint(), tokenBean.getBucket().getBucketName());
//上传图片,需要根据自己的逻辑传参数
ossService.asyncPutImage(图片在阿里上的存储路径, 本地路径, ...);
5.回调处理图片逻辑
/**
* 对图片上传回来的数据进行处理
* @param data
*/
@Override
public void getPicData(PutObjectResult data, String oldPath) {
Gson gson = new Gson();
OssUploadImage uploadImage = gson.fromJson(data.getServerCallbackReturnBody(), OssUploadImage.class);
........逻辑自己写吧
}
~~喵印
网友评论