Skip to content

Commit 8900188

Browse files
author
jordanqin
committed
update qcloud sdk to 5.9.39
1 parent ec8964a commit 8900188

File tree

5 files changed

+148
-24
lines changed

5 files changed

+148
-24
lines changed

QCloudCosXml/cos-android-base/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
minSdkVersion 15
77
targetSdkVersion 28
88

9-
versionCode 50938
10-
versionName '5.9.36'
9+
versionCode 50939
10+
versionName '5.9.37'
1111

1212
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1313

QCloudCosXml/cos-android-base/src/main/java/com/tencent/cos/xml/CosXmlBaseService.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -451,30 +451,34 @@ protected <T1 extends CosXmlRequest, T2 extends CosXmlResult> boolean buildHttpR
451451
}
452452

453453
String networkClientType = null;
454-
if(config.isEnableQuic()) {
455-
if(cosXmlRequest.getNetworkType() == null){
456-
if(config.networkSwitchStrategy() == CosXmlServiceConfig.RequestNetworkStrategy.Aggressive){
457-
if(isNetworkSwitch){
458-
networkClientType = OkHttpClientImpl.class.getName();
459-
} else {
460-
networkClientType = "com.tencent.qcloud.quic.QuicClientImpl";
454+
if(config.getCustomizeNetworkClient() != null){
455+
networkClientType = config.getCustomizeNetworkClient().getClass().getName();
456+
} else {
457+
if(config.isEnableQuic()) {
458+
if(cosXmlRequest.getNetworkType() == null){
459+
if(config.networkSwitchStrategy() == CosXmlServiceConfig.RequestNetworkStrategy.Aggressive){
460+
if(isNetworkSwitch){
461+
networkClientType = OkHttpClientImpl.class.getName();
462+
} else {
463+
networkClientType = "com.tencent.qcloud.quic.QuicClientImpl";
464+
}
465+
} else if(config.networkSwitchStrategy() == CosXmlServiceConfig.RequestNetworkStrategy.Conservative){
466+
if(isNetworkSwitch){
467+
networkClientType = "com.tencent.qcloud.quic.QuicClientImpl";
468+
} else {
469+
networkClientType = OkHttpClientImpl.class.getName();
470+
}
461471
}
462-
} else if(config.networkSwitchStrategy() == CosXmlServiceConfig.RequestNetworkStrategy.Conservative){
463-
if(isNetworkSwitch){
464-
networkClientType = "com.tencent.qcloud.quic.QuicClientImpl";
465-
} else {
472+
} else {
473+
if(cosXmlRequest.getNetworkType() == CosXmlRequest.RequestNetworkType.OKHTTP){
466474
networkClientType = OkHttpClientImpl.class.getName();
475+
} else if(cosXmlRequest.getNetworkType() == CosXmlRequest.RequestNetworkType.QUIC){
476+
networkClientType = "com.tencent.qcloud.quic.QuicClientImpl";
467477
}
468478
}
469479
} else {
470-
if(cosXmlRequest.getNetworkType() == CosXmlRequest.RequestNetworkType.OKHTTP){
471-
networkClientType = OkHttpClientImpl.class.getName();
472-
} else if(cosXmlRequest.getNetworkType() == CosXmlRequest.RequestNetworkType.QUIC){
473-
networkClientType = "com.tencent.qcloud.quic.QuicClientImpl";
474-
}
480+
networkClientType = OkHttpClientImpl.class.getName();
475481
}
476-
} else {
477-
networkClientType = OkHttpClientImpl.class.getName();
478482
}
479483

480484
QCloudHttpRequest<T2> httpRequest = buildHttpRequest(cosXmlRequest, cosXmlResult, isNetworkSwitch, networkClientType);

QCloudCosXml/cos-android/src/androidTest/java/com/tencent/cos/xml/customize_network/CustomizeNetworkClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.tencent.cos.xml.customize_network;
22

33
import static com.tencent.cos.xml.core.TestConst.PERSIST_BUCKET_BIG_OBJECT_SIZE;
4+
import static com.tencent.cos.xml.core.TestUtils.filePath;
45
import static com.tencent.cos.xml.core.TestUtils.getContext;
5-
import static com.tencent.cos.xml.core.TestUtils.smallFilePath;
66

77
import android.text.TextUtils;
88
import android.util.Log;
@@ -63,7 +63,7 @@ public void testBasePutObject() {
6363
CosXmlSimpleService cosXmlSimpleService = newCustomizeNetworkService();
6464
PutObjectRequest request = new PutObjectRequest(TestConst.PERSIST_BUCKET,
6565
TestConst.PERSIST_BUCKET_SMALL_OBJECT_PATH,
66-
smallFilePath());
66+
filePath("do_not_remove/small_object.jpg", 1024 * 1024));
6767
final TestLocker testLocker = new TestLocker();
6868
cosXmlSimpleService.basePutObjectAsync(request, new CosXmlResultListener() {
6969
@Override

QCloudCosXml/cos-android/src/androidTest/java/com/tencent/cos/xml/transfer/UploadTest.java

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import androidx.core.content.FileProvider;
4242
import androidx.test.ext.junit.runners.AndroidJUnit4;
4343

44+
import com.tencent.cos.xml.CosXmlService;
4445
import com.tencent.cos.xml.CosXmlServiceConfig;
4546
import com.tencent.cos.xml.CosXmlSimpleService;
4647
import com.tencent.cos.xml.core.MyOnSignatureListener;
@@ -65,21 +66,27 @@
6566
import com.tencent.cos.xml.utils.DigestUtils;
6667
import com.tencent.cos.xml.utils.UrlUtil;
6768
import com.tencent.qcloud.core.auth.COSXmlSignSourceProvider;
69+
import com.tencent.qcloud.core.auth.SessionQCloudCredentials;
6870
import com.tencent.qcloud.core.auth.ShortTimeCredentialProvider;
6971
import com.tencent.qcloud.core.http.HttpTaskMetrics;
7072
import com.tencent.qcloud.core.logger.QCloudLogger;
7173
import com.tencent.qcloud.core.util.Base64Utils;
7274
import com.tencent.qcloud.core.util.QCloudStringUtils;
7375

76+
import org.json.JSONException;
77+
import org.json.JSONObject;
7478
import org.junit.Assert;
7579
import org.junit.Rule;
7680
import org.junit.Test;
7781
import org.junit.rules.ErrorCollector;
7882
import org.junit.runner.RunWith;
7983

84+
import java.io.BufferedReader;
8085
import java.io.File;
8186
import java.io.IOException;
8287
import java.io.InputStream;
88+
import java.io.InputStreamReader;
89+
import java.net.HttpURLConnection;
8390
import java.net.MalformedURLException;
8491
import java.net.URL;
8592
import java.nio.file.Files;
@@ -1879,4 +1886,117 @@ public void run() {
18791886
uploadLocker.lock();
18801887
TestUtils.assertCOSXMLTaskSuccess(cosxmlUploadTask);
18811888
}
1889+
1890+
public void testKeyAndCredentialsUpload() throws JSONException {
1891+
// 假设需要上传的文件本地路径为filePath
1892+
String filePath = "/path/to/your/file.txt";
1893+
1894+
// 1、从服务端请求上传和签名信息
1895+
File file = new File(filePath);
1896+
// getKeyAndCredentials方法见下一个代码块
1897+
JSONObject keyAndCredentials = getKeyAndCredentials(file.getName());
1898+
String region = keyAndCredentials.getString("region");
1899+
String bucket = keyAndCredentials.getString("bucket");
1900+
String cosKey = keyAndCredentials.getString("key");
1901+
long startTime = keyAndCredentials.getLong("startTime");
1902+
long expiredTime = keyAndCredentials.getLong("expiredTime");
1903+
JSONObject credentials = keyAndCredentials.getJSONObject("credentials");
1904+
String tmpSecretId = credentials.getString("tmpSecretId");
1905+
String tmpSecretKey = credentials.getString("tmpSecretKey");
1906+
String sessionToken = credentials.getString("sessionToken");
1907+
1908+
// 2、初始化 COS SDK: CosXmlService和TransferManager
1909+
// 创建 CosXmlServiceConfig 对象,根据需要修改默认的配置参数
1910+
CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig.Builder()
1911+
.setRegion(region)
1912+
.isHttps(true) // 使用 HTTPS 请求, 默认为 HTTP 请求
1913+
.builder();
1914+
// 初始化一个 CosXmlService 的实例,可以不设置临时密钥回调
1915+
CosXmlService cosXmlService = new CosXmlService(getContext(), serviceConfig);
1916+
// 初始化 TransferConfig,这里使用默认配置,如果需要定制,请参考 SDK 接口文档
1917+
TransferConfig transferConfig = new TransferConfig.Builder().build();
1918+
// 初始化 TransferManager
1919+
TransferManager transferManager = new TransferManager(cosXmlService, transferConfig);
1920+
1921+
// 3、进行上传
1922+
PutObjectRequest putRequest = new PutObjectRequest(bucket, cosKey, filePath);
1923+
SessionQCloudCredentials sessionQCloudCredentials = new SessionQCloudCredentials(tmpSecretId, tmpSecretKey,
1924+
sessionToken, startTime, expiredTime);
1925+
putRequest.setCredential(sessionQCloudCredentials);
1926+
COSXMLUploadTask uploadTask = transferManager.upload(putRequest, null);
1927+
//设置上传进度回调
1928+
uploadTask.setCosXmlProgressListener(new CosXmlProgressListener() {
1929+
@Override
1930+
public void onProgress(long complete, long target) {
1931+
// todo Do something to update progress...
1932+
}
1933+
});
1934+
//设置返回结果回调
1935+
uploadTask.setCosXmlResultListener(new CosXmlResultListener() {
1936+
@Override
1937+
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
1938+
COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =
1939+
(COSXMLUploadTask.COSXMLUploadTaskResult) result;
1940+
}
1941+
1942+
// 如果您使用 kotlin 语言来调用,请注意回调方法中的异常是可空的,否则不会回调 onFail 方法,即:
1943+
// clientException 的类型为 CosXmlClientException?,serviceException 的类型为 CosXmlServiceException?
1944+
@Override
1945+
public void onFail(CosXmlRequest request,
1946+
@Nullable CosXmlClientException clientException,
1947+
@Nullable CosXmlServiceException serviceException) {
1948+
if (clientException != null) {
1949+
clientException.printStackTrace();
1950+
} else {
1951+
serviceException.printStackTrace();
1952+
}
1953+
}
1954+
});
1955+
}
1956+
1957+
/**
1958+
* 获取上传和签名信息
1959+
*
1960+
* @param filename 文件名
1961+
* @return 上传和签名信息
1962+
*/
1963+
private JSONObject getKeyAndCredentials(String filename) {
1964+
// 获取上传和签名信息
1965+
HttpURLConnection getConnection = null;
1966+
try {
1967+
//上面搭建的临时密钥服务(正式环境 请替换成正式的业务url)
1968+
URL url = new URL("http://X.X.X.X:3000/getKeyAndCredentials?filename=" + filename);
1969+
getConnection = (HttpURLConnection) url.openConnection();
1970+
getConnection.setRequestMethod("GET");
1971+
1972+
int responseCode = getConnection.getResponseCode();
1973+
if (responseCode == HttpURLConnection.HTTP_OK) {
1974+
BufferedReader reader = new BufferedReader(new InputStreamReader(getConnection.getInputStream()));
1975+
StringBuilder stringBuilder = new StringBuilder();
1976+
String line;
1977+
while ((line = reader.readLine()) != null) {
1978+
stringBuilder.append(line);
1979+
}
1980+
reader.close();
1981+
JSONObject jsonObject;
1982+
try {
1983+
// 服务端接口需要返回:上传的存储桶、地域、随机路径的对象键、临时密钥
1984+
jsonObject = new JSONObject(stringBuilder.toString());
1985+
return jsonObject;
1986+
} catch (JSONException e) {
1987+
e.printStackTrace();
1988+
}
1989+
} else {
1990+
Log.e("getKeyAndCredentials", "getKeyAndCredentials HTTP error code: " + responseCode);
1991+
}
1992+
} catch (IOException e) {
1993+
e.printStackTrace();
1994+
Log.e("getKeyAndCredentials", "getKeyAndCredentials Error sending GET request: " + e.getMessage());
1995+
} finally {
1996+
if (getConnection != null) {
1997+
getConnection.disconnect();
1998+
}
1999+
}
2000+
return null;
2001+
}
18822002
}

QCloudCosXml/version.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
*/
2323

2424
ext {
25-
cosSdkVersionCode = 50941
26-
cosSdkVersionName = '5.9.38'
25+
cosSdkVersionCode = 50942
26+
cosSdkVersionName = '5.9.39'
2727
}

0 commit comments

Comments
 (0)