Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wushuai1415 committed Oct 20, 2021
2 parents 0183159 + 2a1679f commit dfceb5d
Show file tree
Hide file tree
Showing 23 changed files with 1,343 additions and 72 deletions.
62 changes: 45 additions & 17 deletions app/src/main/java/com/alibaba/oss/app/service/OssService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import com.alibaba.oss.app.Config;
Expand All @@ -14,13 +13,12 @@
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.OSSConstants;
import com.alibaba.sdk.android.oss.common.OSSLog;
import com.alibaba.sdk.android.oss.common.auth.OSSCustomSignerCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.common.utils.BinaryUtil;
import com.alibaba.sdk.android.oss.common.utils.OSSUtils;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.ResumableDownloadResult;
import com.alibaba.sdk.android.oss.model.CompleteMultipartUploadResult;
import com.alibaba.sdk.android.oss.model.CreateBucketRequest;
import com.alibaba.sdk.android.oss.model.DeleteBucketRequest;
Expand All @@ -34,9 +32,9 @@
import com.alibaba.sdk.android.oss.model.ImagePersistResult;
import com.alibaba.sdk.android.oss.model.ListObjectsRequest;
import com.alibaba.sdk.android.oss.model.ListObjectsResult;
import com.alibaba.sdk.android.oss.model.ResumableDownloadRequest;
import com.alibaba.sdk.android.oss.model.MultipartUploadRequest;
import com.alibaba.sdk.android.oss.model.OSSRequest;
import com.alibaba.sdk.android.oss.model.ObjectMetadata;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.alibaba.sdk.android.oss.model.ResumableUploadRequest;
Expand All @@ -45,28 +43,15 @@
import com.alibaba.sdk.android.oss.model.TriggerCallbackResult;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.alibaba.oss.app.Config.OSS_ACCESS_KEY_ID;
import static com.alibaba.oss.app.Config.OSS_ACCESS_KEY_SECRET;

import com.alibaba.oss.app.OSSWrapper;

/**
* Created by mOss on 2015/12/7 0007.
* 支持普通上传,普通下载
Expand Down Expand Up @@ -380,6 +365,49 @@ public void onFailure(ResumableUploadRequest request, ClientException clientExce
});
}

public void asyncResumableDownload(String downloadPath) {
Map<String, String> header = new HashMap<>();
ResumableDownloadRequest request = new ResumableDownloadRequest(Config.BUCKET_NAME, "landscape-painting1.jpeg", downloadPath + "/landscape-painting.jpeg");
request.setEnableCheckPoint(true);
// request.setRange(new Range(0, 1024 * 1024));
request.setPartSize(100 * 1024);
request.setRequestHeader(header);
request.setCheckPointFilePath(downloadPath);
request.setCRC64(OSSRequest.CRC64Config.YES);
request.setProgressListener(new OSSProgressCallback() {
@Override
public void onProgress(Object request, long currentSize, long totalSize) {
Log.i("MultipartDownload", "currentSize: " + currentSize + ", totalSize: " + totalSize);
if (totalSize == 0) {
return;
}
int progress = (int) (100 * currentSize / totalSize);
mDisplayer.updateProgress(progress);
mDisplayer.displayInfo("上传进度: " + String.valueOf(progress) + "%");
}
});
final long start = System.currentTimeMillis();
final OSSAsyncTask task = mOss.asyncResumableDownload(request, new OSSCompletedCallback<ResumableDownloadRequest, ResumableDownloadResult>() {
@Override
public void onSuccess(ResumableDownloadRequest request, ResumableDownloadResult result) {
Log.i("MultipartDownload", result.getMetadata().toString());
long time = System.currentTimeMillis() - start;
Log.i("time", time + "");
}

@Override
public void onFailure(ResumableDownloadRequest request, ClientException clientException, ServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
Log.i("clientException", "clientException");
} else if (serviceException != null) {
Log.i("serviceException", "serviceException");
serviceException.printStackTrace();
}
}
});
}

// If the bucket is private, the signed URL is required for the access.
// Expiration time is specified in the signed URL.
public void presignURLWithBucketAndKey(final String objectKey) {
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/alibaba/oss/app/view/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
import com.alibaba.sdk.android.oss.common.auth.OSSAuthCredentialsProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -346,6 +348,15 @@ public void onClick(View v) {
}
});

final String filePath = getFilesDir() + "";
Button multipartDownloadButton = (Button) findViewById(R.id.resumableDownload);
multipartDownloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mService.asyncResumableDownload(filePath);
}
});

Button resumableButton = (Button) findViewById(R.id.resumableUpload);
resumableButton.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,18 @@
android:layout_height="match_parent"
android:text="@string/resumableUpload" />


</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/resumableDownload"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_marginLeft="20dp"
android:layout_height="match_parent"
android:text="@string/multipartDownload" />
</LinearLayout>

<LinearLayout
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<string name="manageBuckets">manage</string>
<string name="ossSign">ossSign</string>
<string name="resumableUpload">resumable</string>
<string name="multipartDownload">resumableDownload</string>
<string name="multipartUpload">multipart</string>
<string name="cutomSign">cutomsign</string>
<string name="batch_upload">batch</string>
Expand Down
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Github地址:https://github.com/aliyun/aliyun-oss-android-sdk

更新日志:

2021/08/18
- release 2.9.9
1. Delete the useless OSSSQLiteHelper class

2021/07/23
- release 2.9.8
1. Migrate from jcenter to mavenCentral
Expand Down
2 changes: 1 addition & 1 deletion oss-android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ android {
minSdkVersion 14
targetSdkVersion 29
versionCode 40
versionName "2.9.8"
versionName "2.9.9"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public abstract class BaseTestCase {
protected static OSS oss;

@Rule
public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.ACCESS_WIFI_STATE);

abstract void initTestData() throws Exception;
void initOSSClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import com.alibaba.sdk.android.oss.model.DeleteBucketResult;
import com.alibaba.sdk.android.oss.model.DeleteObjectRequest;
import com.alibaba.sdk.android.oss.model.DeleteObjectResult;
import com.alibaba.sdk.android.oss.model.ResumableDownloadRequest;
import com.alibaba.sdk.android.oss.model.ResumableDownloadResult;
import com.alibaba.sdk.android.oss.model.GetBucketACLRequest;
import com.alibaba.sdk.android.oss.model.GetBucketACLResult;
import com.alibaba.sdk.android.oss.model.GetBucketInfoRequest;
Expand Down Expand Up @@ -76,7 +78,6 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

Expand Down Expand Up @@ -866,4 +867,24 @@ public void onFailure(CompleteMultipartUploadRequest request, ClientException cl
this.serviceException = serviceException;
}
}

public final static class TestResumableDownloadCallback implements OSSCompletedCallback<ResumableDownloadRequest, ResumableDownloadResult> {

public ResumableDownloadRequest request;
public ResumableDownloadResult result;
public ClientException clientException;
public ServiceException serviceException;
@Override
public void onSuccess(ResumableDownloadRequest request, ResumableDownloadResult result) {
this.request = request;
this.result = result;
}

@Override
public void onFailure(ResumableDownloadRequest request, ClientException clientException, ServiceException serviceException) {
this.request = request;
this.clientException = clientException;
this.serviceException = serviceException;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.alibaba.sdk.android.oss.model.ListObjectsResult;
import com.alibaba.sdk.android.oss.model.MultipartUpload;
import com.alibaba.sdk.android.oss.model.OSSObjectSummary;
import com.alibaba.sdk.android.oss.model.Range;

import java.io.FileDescriptor;
import java.io.IOException;
Expand Down Expand Up @@ -72,6 +73,16 @@ public static void checkFileMd5(OSS oss, String bucket, String objectKey, String
assertEquals(200, getRs.getStatusCode());
}

public static void checkFileMd5(OSS oss, String bucket, String objectKey, Range range, String filePath) throws IOException, NoSuchAlgorithmException, ClientException, ServiceException {
GetObjectRequest getRq = new GetObjectRequest(bucket, objectKey);
getRq.setRange(range);
GetObjectResult getRs = oss.getObject(getRq);
String localMd5 = BinaryUtil.calculateMd5Str(filePath);
String remoteMd5 = getMd5(getRs);
assertEquals(true, localMd5.equals(remoteMd5));
assertNotNull(getRs);
}

public static void checkFileMd5(OSS oss, String bucket, String objectKey, FileDescriptor fileDescriptor) throws IOException, NoSuchAlgorithmException, ClientException, ServiceException {
GetObjectRequest getRq = new GetObjectRequest(bucket, objectKey);
GetObjectResult getRs = oss.getObject(getRq);
Expand Down
Loading

0 comments on commit dfceb5d

Please sign in to comment.