Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev jingdan #50

Merged
merged 31 commits into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a70d134
add write log feature
Aug 18, 2017
b447002
add rm test
Aug 18, 2017
b2227f2
add test case
Aug 18, 2017
bf24863
add test case
Aug 18, 2017
ad807b8
add file case
Aug 18, 2017
ae4ef98
add 该接口已废弃
Aug 22, 2017
684a76b
add revert test data
Aug 22, 2017
dfe047b
add 删除 无用跑测试配置
Aug 22, 2017
560475e
add 修改代码
Aug 22, 2017
4d4db1b
add 增加日志信息
Aug 22, 2017
efe548d
Merge branch 'master' of https://github.com/aliyun/aliyun-oss-android…
Aug 22, 2017
efff217
add 调整为测试常量
Aug 22, 2017
1b8cab7
add 修改注释文案
Aug 23, 2017
955376a
add 去除无用代码,之前是mns相关代码在调用
Aug 24, 2017
24a2316
add 内部使用的工具方法,都没有被调用, 故删除
Aug 24, 2017
d8ace49
add 测试case 完善 代码覆盖率up
Aug 24, 2017
c55e29f
add test case
Aug 28, 2017
edffeee
add 代码部分优化
Aug 28, 2017
d525641
去除无调用代码
Aug 28, 2017
3ff5a98
获取信息 采用 android 信息
Aug 28, 2017
f204301
add 还原测试信息
Aug 28, 2017
fb1fde1
add test 文件还原测试数据
Aug 28, 2017
a635eb9
add test case -> 95%
Aug 29, 2017
112b0f7
add change OSSLogToFileUtils class package
Sep 1, 2017
2eab512
add 注释
Sep 1, 2017
edd3d31
add readme 补齐 日志部分
Sep 1, 2017
bc6feb0
add improve code by review
Sep 5, 2017
1e03fa4
add getObjectTest response stream close
Sep 5, 2017
ade8100
rename log
Sep 7, 2017
6a86719
reset
Sep 8, 2017
d52bb12
Merge branch 'master' of https://github.com/aliyun/aliyun-oss-android…
Sep 8, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,19 @@ sample目录: [点击查看](https://github.com/aliyun/aliyun-oss-android-sdk/tr

```java
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider("<StsToken.AccessKeyId>", "<StsToken.SecretKeyId>", "<StsToken.SecurityToken>");

//该配置类如果不设置,会有默认配置,具体可看该类
ClientConfiguration conf = new ClientConfiguration();
conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒
conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒
conf.setMaxConcurrentRequest(5); // 最大并发请求数,默认5个
conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次
OSSLog.enableLog(); //这个开启会支持写入手机sd卡中的一份日志文件位置在SDCard_path\OSSLog\logs.csv

OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider("<StsToken.AccessKeyId>", "<StsToken.SecretKeyId>", "<StsToken.SecurityToken>");

OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider);
OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider,conf);
Copy link

@baiyubin baiyubin Sep 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • credentialProvider,conf ->credentialProvider, conf conf前添加一个空格。其它地方不再提醒。公司Java代码规范请参看集团开发规

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

```

### STEP-2. 上传文件
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,17 @@ We recommend STS authentication mode to initialize the OSSClient on mobile. For
```java
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";

//if null , default will be init
ClientConfiguration conf = new ClientConfiguration();
conf.setConnectionTimeout(15 * 1000); // connction time out default 15s
conf.setSocketTimeout(15 * 1000); // socket timeout,default 15s
conf.setMaxConcurrentRequest(5); // synchronous request number,default 5
conf.setMaxErrorRetry(2); // retry,default 2
OSSLog.enableLog(); //write local log file ,path is SDCard_path\OSSLog\logs.csv

OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider("<StsToken.AccessKeyId>", "<StsToken.SecretKeyId>", "<StsToken.SecurityToken>");

OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider);
OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider,conf);
```

### Step-2. Upload a file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider("<StsToken.AccessKeyId>", "<StsToken.SecretKeyId>", "<StsToken.SecurityToken>");

ClientConfiguration conf = new ClientConfiguration();
conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒
conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒
conf.setMaxConcurrentRequest(5); // 最大并发请求书,默认5个
conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次
OSSLog.enableLog();
OSSLog.enableLog(); //这个开启会支持写入手机sd卡中的一份日志文件位置在SD_path\OSSLog\logs.csv

oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider, conf);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.model.GetObjectRequest;
import com.alibaba.sdk.android.oss.model.GetObjectResult;
import com.alibaba.sdk.android.oss.model.HeadObjectRequest;
import com.alibaba.sdk.android.oss.model.HeadObjectResult;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;

Expand All @@ -25,19 +27,32 @@
public class ConfigurationTest extends AndroidTestCase {

private OSS oss;
private String objectKey = "file1m";
private String filePath = OSSTestConfig.FILE_DIR + "file1m";


@Override
public void setUp() throws Exception {
OSSTestConfig.instance(getContext());
if (oss == null) {
Thread.sleep(5 * 1000); // for logcat initialization
OSSLog.enableLog();
oss = new OSSClient(getContext(), OSSTestConfig.ENDPOINT, OSSTestConfig.credentialProvider);
}
}

public void testUpdateCredentialProvider() throws Exception{

oss.updateCredentialProvider(OSSTestConfig.plainTextAKSKcredentialProvider);

HeadObjectRequest head = new HeadObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, "file1m");

HeadObjectResult headResult = oss.headObject(head);

assertNotNull(headResult.getMetadata().getContentType());
assertEquals(1024 * 1000, headResult.getMetadata().getContentLength());

//revert
oss.updateCredentialProvider(OSSTestConfig.credentialProvider);
}

public void testCnameSetting() throws Exception {

oss = new OSSClient(getContext(), OSSTestConfig.ANDROID_TEST_CNAME, OSSTestConfig.credentialProvider);
Expand All @@ -64,11 +79,38 @@ public void testCnameSetting() throws Exception {
public void testCustomExcludeCname() throws Exception {

List cnameExcludeList = new ArrayList();
cnameExcludeList.add("xxyycc.com");
cnameExcludeList.add(OSSTestConfig.EXCLUDE_HOST);
ClientConfiguration conf = new ClientConfiguration();
conf.setCustomCnameExcludeList(cnameExcludeList);

oss = new OSSClient(getContext(), OSSTestConfig.ENDPOINT, OSSTestConfig.credentialProvider, conf);

GetObjectRequest get = new GetObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, "file1m");
GetObjectResult getResult = oss.getObject(get);
assertEquals(200, getResult.getStatusCode());

PutObjectRequest put = new PutObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, "file1m", OSSTestConfig.FILE_DIR + "/file1m");
PutObjectResult putResult = oss.putObject(put);
assertEquals(200, putResult.getStatusCode());

String url = oss.presignConstrainedObjectURL(OSSTestConfig.ANDROID_TEST_BUCKET, "file1m", 30 * 60);
OSSLog.logD("Presiged constraintdd url: " + url);
Request request = new Request.Builder().url(url).build();
Response response = new OkHttpClient().newCall(request).execute();
assertEquals(200, response.code());

url = oss.presignPublicObjectURL(OSSTestConfig.PUBLIC_READ_BUCKET, "file1m");
assertEquals("http://" + OSSTestConfig.PUBLIC_READ_BUCKET+"." + OSSTestConfig.EXCLUDE_HOST + "/file1m", url);
}

public void testCustomExcludeCnameWithHttp() throws Exception {

List cnameExcludeList = new ArrayList();
cnameExcludeList.add(OSSTestConfig.EXCLUDE_HOST_WITH_HTTP);
ClientConfiguration conf = new ClientConfiguration();
conf.setCustomCnameExcludeList(cnameExcludeList);

oss = new OSSClient(getContext(), "http://xxyycc.com", OSSTestConfig.credentialProvider, conf);
oss = new OSSClient(getContext(), OSSTestConfig.ENDPOINT, OSSTestConfig.credentialProvider, conf);

GetObjectRequest get = new GetObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, "file1m");
GetObjectResult getResult = oss.getObject(get);
Expand All @@ -85,6 +127,26 @@ public void testCustomExcludeCname() throws Exception {
assertEquals(200, response.code());

url = oss.presignPublicObjectURL(OSSTestConfig.PUBLIC_READ_BUCKET, "file1m");
assertEquals("http://" + OSSTestConfig.PUBLIC_READ_BUCKET + ".xxyycc.com" + "/file1m", url);
assertEquals("http://" + OSSTestConfig.PUBLIC_READ_BUCKET+"." + OSSTestConfig.EXCLUDE_HOST + "/file1m", url);
}

public void testCustomExcludeCnameError(){
try {
List cnameExcludeList = new ArrayList();
ClientConfiguration conf = new ClientConfiguration();
conf.setCustomCnameExcludeList(cnameExcludeList);
}catch (Exception e){
assertTrue(true);
}
}

public void testProxySettings() throws Exception{
ClientConfiguration conf = new ClientConfiguration();
conf.setProxyHost(OSSTestConfig.PROXY);//当前自己的机器地址
conf.setProxyPort(OSSTestConfig.PROXY_PORT);
oss = new OSSClient(getContext(), OSSTestConfig.ENDPOINT, OSSTestConfig.credentialProvider, conf);
GetObjectRequest get = new GetObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, "file1m");
GetObjectResult getResult = oss.getObject(get);
assertEquals(200, getResult.getStatusCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
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.common.OSSConstants;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.common.OSSLog;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSFederationCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSFederationToken;
import com.alibaba.sdk.android.oss.common.utils.IOUtils;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.CopyObjectRequest;
import com.alibaba.sdk.android.oss.model.CopyObjectResult;
Expand All @@ -23,17 +19,13 @@
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 okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Created by zhouzhuo on 11/25/15.
Expand All @@ -42,10 +34,12 @@ public class ManageObjectTest extends AndroidTestCase {
private OSS oss;
private String objectKey = "file1m";
private String filePath = OSSTestConfig.FILE_DIR + "file1m";
private String TEST_ETAG = "7E868A8A0AD0493DD9545129EFD51C45-4";


@Override
public void setUp() throws Exception {
OSSTestConfig.instance(getContext());
if (oss == null) {
Thread.sleep(5 * 1000); // for logcat initialization
OSSLog.enableLog();
Expand Down Expand Up @@ -88,7 +82,7 @@ private void initLocalFile(){
}
}

public void uploadObjectForTest() throws Exception {
public void testUploadObjectForTest() throws Exception {
PutObjectRequest put = new PutObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET,
objectKey, filePath);

Expand Down Expand Up @@ -116,7 +110,7 @@ public void testDeleteObject() throws Exception {
} catch (ServiceException e) {
assertEquals(404, e.getStatusCode());
}
uploadObjectForTest();
testUploadObjectForTest();
}

public void testDeleteAsync() throws Exception {
Expand All @@ -142,7 +136,45 @@ public void testDeleteAsync() throws Exception {
} catch (ServiceException e) {
assertEquals(404, e.getStatusCode());
}
uploadObjectForTest();
testUploadObjectForTest();
}

public void testAsyncCopyObject() throws Exception {
DeleteObjectRequest delete = new DeleteObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, "testCopy");
oss.deleteObject(delete);

CopyObjectRequest copyObjectRequest = new CopyObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, objectKey,
OSSTestConfig.ANDROID_TEST_BUCKET, "testCopy");

copyObjectRequest.setServerSideEncryption(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);

SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
Date myDate1 = dateFormat1.parse("2017-01-01");
copyObjectRequest.setModifiedSinceConstraint(myDate1);

copyObjectRequest.setUnmodifiedSinceConstraint(new Date());

ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType("application/binary-copy");

copyObjectRequest.setNewObjectMetadata(objectMetadata);

OSSTestConfig.TestCopyObjectCallback callback = new OSSTestConfig.TestCopyObjectCallback();


OSSAsyncTask task = oss.asyncCopyObject(copyObjectRequest,callback);

task.waitUntilFinished();

assertNull(callback.serviceException);
assertNotNull(callback.result.getETag());
assertNotNull(callback.result.getLastModified());


HeadObjectRequest head = new HeadObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, "testCopy");
HeadObjectResult result = oss.headObject(head);

assertEquals("application/binary-copy", result.getMetadata().getContentType());
}

public void testCopyObject() throws Exception {
Expand All @@ -152,6 +184,64 @@ public void testCopyObject() throws Exception {
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, objectKey,
OSSTestConfig.ANDROID_TEST_BUCKET, "testCopy");

copyObjectRequest.setServerSideEncryption(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);

SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
Date myDate1 = dateFormat1.parse("2017-01-01");
copyObjectRequest.setModifiedSinceConstraint(myDate1);

copyObjectRequest.setUnmodifiedSinceConstraint(new Date());

ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType("application/binary-copy");

copyObjectRequest.setNewObjectMetadata(objectMetadata);

CopyObjectResult copyResult = oss.copyObject(copyObjectRequest);
assertNotNull(copyResult.getETag());
assertNotNull(copyResult.getLastModified());


HeadObjectRequest head = new HeadObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, "testCopy");
HeadObjectResult result = oss.headObject(head);

assertEquals("application/binary-copy", result.getMetadata().getContentType());
}

public void testCopyObjectWithMatchEtag() throws Exception{

CopyObjectRequest copyObjectRequest = new CopyObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, objectKey,
OSSTestConfig.ANDROID_TEST_BUCKET, "testCopy");

copyObjectRequest.clearMatchingETagConstraints();

List<String> etags = new ArrayList<String>();
etags.add(TEST_ETAG);
copyObjectRequest.setMatchingETagConstraints(etags);

ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType("application/binary-copy");

copyObjectRequest.setNewObjectMetadata(objectMetadata);

try {
oss.copyObject(copyObjectRequest);
}catch (ServiceException e){ //412 指定的etag 与source object 不符
assertTrue(true);
}
}

public void testCopyObjectWithNoMatchEtag() throws Exception{

CopyObjectRequest copyObjectRequest = new CopyObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, objectKey,
OSSTestConfig.ANDROID_TEST_BUCKET, "testCopy");

copyObjectRequest.clearNonmatchingETagConstraints();

List<String> etags = new ArrayList<String>();
etags.add("1234567890");
copyObjectRequest.setNonmatchingETagConstraints(etags);

ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType("application/binary-copy");

Expand All @@ -168,6 +258,20 @@ public void testCopyObject() throws Exception {
assertEquals("application/binary-copy", result.getMetadata().getContentType());
}

public void testAsyncHeadObject() throws Exception {
HeadObjectRequest head = new HeadObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, "file1m");

OSSTestConfig.TestHeadObjectCallback callback = new OSSTestConfig.TestHeadObjectCallback();

OSSAsyncTask<HeadObjectResult> task = oss.asyncHeadObject(head, callback);

task.waitUntilFinished();

assertNull(callback.serviceException);
assertNotNull(callback.result.getMetadata().getContentType());
assertEquals(1024 * 1000, callback.result.getMetadata().getContentLength());
}

public void testHeadObject() throws Exception {
HeadObjectRequest head = new HeadObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, "file1m");

Expand Down
Loading