1. 阿里云oss官网,资料比较齐,有时间应该系统看一下。
https://help.aliyun.com/document_detail/106959.html?spm=a2c4g.11186623.6.1162.56754b86vIbv3t
2.初次使用坑比较多,主要如下:
manifest增加app权限:
<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.MOUNT_UNMOUNT_FILESYSTEMS" />
build.gradle增加
compile 'com.aliyun.dpa:oss-android-sdk:+'
proguard-rules.pro增加
-keep class com.alibaba.sdk.android.oss.** { *; }
-dontwarn okio.**
-dontwarn org.apache.commons.codec.binary.**
使用AK下载oss
OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(OSS_ACCESS_KEY_ID,OSS_ACCESS_KEY_SECRET);
mClient = new OSSClient(MainApplication.getContext(), OSS_ENDPOINT, credentialProvider);
//主流畅
GetObjectRequest get = new GetObjectRequest("autoai-oss-model", "ace_model/1.0.0_xxx.zip");
getCallback = new TestGetCallback();
try {
mClient.getObject(get);
} catch (ClientException e1) {
e1.printStackTrace();
} catch (ServiceException e1) {
e1.printStackTrace();
}
OSSAsyncTask getTask = mClient.asyncGetObject(get, getCallback);
getTask.waitUntilFinished();
public final class TestGetCallback implements OSSCompletedCallback<GetObjectRequest, GetObjectResult> {
public GetObjectRequest request;
public GetObjectResult result;
public ClientException clientException;
public ServiceException serviceException;
@Override
public void onSuccess(GetObjectRequest request, GetObjectResult result) {
Log.v("AvceService","onSuccess");
this.request = request;
this.result = result;
writeFile("/sdcard/file1m");
}
@Override
public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
Log.v("AvceService","onFailure");
this.request = request;
this.clientException = clientExcepion;
this.serviceException = serviceException;
}
}
//cannot be access in the main thread.
public void writeFile(String name)
{
String srcFileBase64Md5 = null;
try {
srcFileBase64Md5 = BinaryUtil.toBase64String(BinaryUtil.calculateMd5(name));
} catch (IOException e) {
e.printStackTrace();
}
Log.i(TAG,""+srcFileBase64Md5);
Log.i(TAG,"haha-- "+getCallback.result.getStatusCode());
assertEquals(200, getCallback.result.getStatusCode());
Log.i(TAG,"haha---");
long length = getCallback.result.getContentLength();
Log.i(TAG,"downloadFile "+length );
byte[] buffer = new byte[(int) length];
int readCount = 0;
InputStream inputStream = getCallback.result.getObjectContent();
Log.i(TAG,""+inputStream);
while (readCount < length) {
try {
readCount += inputStream.read(buffer, readCount, (int) length - readCount);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Log.i(TAG,"new file " );
FileOutputStream fout = new FileOutputStream(name);
Log.i(TAG,"downloadFile -- " );
fout.write(buffer);
fout.close();
} catch (Exception e) {
OSSLog.logInfo(e.toString());
Log.i(TAG,e.toString() );
}
String downloadFileBase64Md5 = null;
try {
downloadFileBase64Md5 = BinaryUtil.toBase64String(BinaryUtil.calculateMd5(name));
} catch (IOException e) {
e.printStackTrace();
}
assertEquals(srcFileBase64Md5, downloadFileBase64Md5);
}
其他:
AK方式。