Skip to content

Commit 3d519b6

Browse files
author
jordanqin
committed
update qcloud sdk to 5.9.34
1 parent be6e35b commit 3d519b6

File tree

12 files changed

+588
-90
lines changed

12 files changed

+588
-90
lines changed

QCloudCosXml/cos-android-base/build.gradle

Lines changed: 3 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 50931
10-
versionName '5.9.31'
9+
versionCode 50932
10+
versionName '5.9.32'
1111

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

@@ -70,6 +70,7 @@ dependencies {
7070

7171
api project(':foundation')
7272
api project(':qcloud-track')
73+
api project(':network-sonar')
7374
}
7475

7576
project.extensions.add('artifactId', 'qcloud-cos-android-base')

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

Lines changed: 389 additions & 81 deletions
Large diffs are not rendered by default.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import java.util.List;
8888
import java.util.Map;
8989
import java.util.Set;
90+
import java.util.UUID;
9091
import java.util.concurrent.Executor;
9192

9293

@@ -427,6 +428,11 @@ protected <T1 extends CosXmlRequest, T2 extends CosXmlResult> T2 execute(T1 cosX
427428
protected <T1 extends CosXmlRequest, T2 extends CosXmlResult> T2 execute(T1 cosXmlRequest, T2 cosXmlResult, boolean internal)
428429
throws CosXmlClientException, CosXmlServiceException {
429430
try {
431+
// 如果有值 说明是task传入的 则以task为准
432+
if(TextUtils.isEmpty(cosXmlRequest.getClientTraceId())){
433+
cosXmlRequest.setClientTraceId(UUID.randomUUID().toString());
434+
}
435+
430436
if(TextUtils.isEmpty(cosXmlRequest.getRegion()) && config != null){
431437
cosXmlRequest.setRegion(config.getRegion());
432438
}
@@ -454,12 +460,15 @@ protected <T1 extends CosXmlRequest, T2 extends CosXmlResult> T2 execute(T1 cosX
454460
HttpResult<T2> httpResult = httpTask.executeNow();
455461

456462
CosTrackService.getInstance().reportRequestSuccess(cosXmlRequest, internal);
463+
CosTrackService.getInstance().reportHttpMetrics(cosXmlRequest);
457464
logRequestMetrics(cosXmlRequest);
458465

459466
return httpResult != null ? httpResult.content() : null;
460467
} catch (QCloudServiceException e) {
468+
CosTrackService.getInstance().reportHttpMetrics(cosXmlRequest);
461469
throw CosTrackService.getInstance().reportRequestServiceException(cosXmlRequest, e, internal);
462470
} catch (QCloudClientException e) {
471+
CosTrackService.getInstance().reportHttpMetrics(cosXmlRequest);
463472
throw CosTrackService.getInstance().reportRequestClientException(cosXmlRequest, e, internal);
464473
}
465474
}
@@ -477,12 +486,14 @@ protected <T1 extends CosXmlRequest, T2 extends CosXmlResult> void schedule(fina
477486
@Override
478487
public void onSuccess(HttpResult<T2> result) {
479488
CosTrackService.getInstance().reportRequestSuccess(cosXmlRequest, internal);
489+
CosTrackService.getInstance().reportHttpMetrics(cosXmlRequest);
480490
logRequestMetrics(cosXmlRequest);
481491
cosXmlResultListener.onSuccess(cosXmlRequest, result.content());
482492
}
483493

484494
@Override
485495
public void onFailure(QCloudClientException clientException, QCloudServiceException serviceException) {
496+
CosTrackService.getInstance().reportHttpMetrics(cosXmlRequest);
486497
logRequestMetrics(cosXmlRequest);
487498
if (clientException != null) {
488499
CosXmlClientException xmlClientException = CosTrackService.getInstance().reportRequestClientException(cosXmlRequest, clientException, internal);
@@ -495,6 +506,10 @@ public void onFailure(QCloudClientException clientException, QCloudServiceExcept
495506
};
496507

497508
try {
509+
if(TextUtils.isEmpty(cosXmlRequest.getClientTraceId())){
510+
cosXmlRequest.setClientTraceId(UUID.randomUUID().toString());
511+
}
512+
498513
if(TextUtils.isEmpty(cosXmlRequest.getRegion()) && config != null){
499514
cosXmlRequest.setRegion(config.getRegion());
500515
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ public abstract class CosXmlRequest{
121121
*/
122122
private QCloudCredentialProvider credentialProvider;
123123

124+
/**
125+
* 标记一次请求的客户端唯一标识,用于日志上报和本地日志记录
126+
*/
127+
private String clientTraceId;
128+
124129
/**
125130
* 设置请求URL
126131
* @param requestURL 请求URL
@@ -598,6 +603,20 @@ public QCloudCredentialProvider getCredentialProvider() {
598603
return credentialProvider;
599604
}
600605

606+
/**
607+
* 标记一次请求的客户端唯一标识,用于日志上报和本地日志记录
608+
*/
609+
public String getClientTraceId() {
610+
return clientTraceId;
611+
}
612+
613+
/**
614+
* 标记一次请求的客户端唯一标识,用于日志上报和本地日志记录
615+
*/
616+
public void setClientTraceId(String clientTraceId) {
617+
this.clientTraceId = clientTraceId;
618+
}
619+
601620
/**
602621
* 获取请求重量级的监听接口
603622
*/

QCloudCosXml/cos-android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ android {
9292
}
9393

9494
// 切换发 normal 或者 slim 包,需要同步修改 pom.artifactId 信息
95-
// defaultPublishConfig "normalRelease"
96-
defaultPublishConfig "slimRelease"
95+
defaultPublishConfig "normalRelease"
96+
// defaultPublishConfig "slimRelease"
9797

9898
compileOptions {
9999
sourceCompatibility = JavaVersion.VERSION_1_8

QCloudCosXml/cos-android/src/androidTest/java/com/tencent/cos/xml/common/OtherTest.java

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@
5151
import org.junit.Test;
5252
import org.junit.runner.RunWith;
5353

54+
import java.io.DataOutputStream;
5455
import java.io.File;
5556
import java.io.FileInputStream;
57+
import java.io.FileOutputStream;
5658
import java.io.IOException;
59+
import java.io.InputStream;
5760
import java.math.BigInteger;
61+
import java.net.HttpURLConnection;
62+
import java.net.URL;
5863
import java.net.URLEncoder;
5964

6065
import okhttp3.MediaType;
@@ -170,7 +175,6 @@ public void onFail(CosXmlClientException exception, CosXmlServiceException servi
170175
}
171176

172177
@Test public void testPresignedRequest() {
173-
174178
PresignedUrlRequest presignedUrlRequest = new PresignedUrlRequest(TestConst.PERSIST_BUCKET, TestConst.PERSIST_BUCKET_SMALL_OBJECT_PATH) {
175179
@Override
176180
public RequestBodySerializer getRequestBody() throws CosXmlClientException {
@@ -198,6 +202,68 @@ public RequestBodySerializer getRequestBody() throws CosXmlClientException {
198202
}
199203
}
200204

205+
public void uploadFile(String targetUrl, String filePath) {
206+
int retryCount = 0;
207+
boolean success = false;
208+
209+
while (!success && retryCount < 3) {
210+
HttpURLConnection connection = null;
211+
DataOutputStream outputStream = null;
212+
FileInputStream fileInputStream = null;
213+
214+
try {
215+
fileInputStream = new FileInputStream(filePath);
216+
217+
URL url = new URL(targetUrl);
218+
connection = (HttpURLConnection) url.openConnection();
219+
220+
connection.setDoInput(true);
221+
connection.setDoOutput(true);
222+
connection.setUseCaches(false);
223+
224+
connection.setRequestMethod("POST");
225+
226+
outputStream = new DataOutputStream(connection.getOutputStream());
227+
int bytesRead;
228+
byte[] buffer = new byte[8192];
229+
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
230+
outputStream.write(buffer, 0, bytesRead);
231+
}
232+
233+
outputStream.flush();
234+
235+
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
236+
if (connection.getResponseCode() >= 500) {
237+
retryCount++;
238+
continue;
239+
} else {
240+
throw new RuntimeException("Server returned HTTP " + connection.getResponseCode()
241+
+ " " + connection.getResponseMessage());
242+
}
243+
}
244+
245+
success = true;
246+
} catch (Exception e) {
247+
retryCount++;
248+
} finally {
249+
try {
250+
if (outputStream != null)
251+
outputStream.close();
252+
if (fileInputStream != null)
253+
fileInputStream.close();
254+
} catch (Exception ignored) {
255+
}
256+
257+
if (connection != null)
258+
connection.disconnect();
259+
}
260+
}
261+
262+
if (!success) {
263+
throw new RuntimeException("Failed to upload file after 3 attempts");
264+
}
265+
}
266+
201267
@Test public void testPresignedDownload() {
202268
PresignedUrlRequest presignedUrlRequest = new PresignedUrlRequest(TestConst.PERSIST_BUCKET, "wechat.png");
203269
presignedUrlRequest.setCosPath("wechat.png");
@@ -209,11 +275,61 @@ public RequestBodySerializer getRequestBody() throws CosXmlClientException {
209275
try {
210276
String signUrl = defaultService.getPresignedURL(presignedUrlRequest);
211277
QCloudLogger.i("QCloudTest", signUrl);
278+
new Thread(() -> {
279+
String localPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/wechat.png";
280+
downloadFile(signUrl, localPath);
281+
}).start();
212282
} catch (CosXmlClientException clientException) {
213283
QCloudLogger.i("QCloudTest", clientException.getMessage());
214284
clientException.printStackTrace();
215285
}
216286
}
287+
public void downloadFile(String fileUrl, String localPath) {
288+
int retryCount = 0;
289+
boolean success = false;
290+
while (!success && retryCount < 3) {
291+
HttpURLConnection connection = null;
292+
InputStream input = null;
293+
FileOutputStream output = null;
294+
try {
295+
URL url = new URL(fileUrl);
296+
connection = (HttpURLConnection) url.openConnection();
297+
connection.connect();
298+
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
299+
if (connection.getResponseCode() >= 500) {
300+
retryCount++;
301+
continue;
302+
} else {
303+
throw new RuntimeException("Server returned HTTP " + connection.getResponseCode()
304+
+ " " + connection.getResponseMessage());
305+
}
306+
}
307+
input = connection.getInputStream();
308+
output = new FileOutputStream(localPath);
309+
byte data[] = new byte[4096];
310+
int count;
311+
while ((count = input.read(data)) != -1) {
312+
output.write(data, 0, count);
313+
}
314+
success = true;
315+
} catch (Exception e) {
316+
retryCount++;
317+
} finally {
318+
try {
319+
if (output != null)
320+
output.close();
321+
if (input != null)
322+
input.close();
323+
} catch (Exception ignored) {
324+
}
325+
if (connection != null)
326+
connection.disconnect();
327+
}
328+
}
329+
if (!success) {
330+
throw new RuntimeException("Failed to download file after 3 attempts");
331+
}
332+
}
217333

218334
@Test public void testPresignedDownloadFail1() {
219335
PresignedUrlRequest presignedUrlRequest = new PresignedUrlRequest("", "wechat.png");

QCloudCosXml/cos-android/src/main/java/com/tencent/cos/xml/transfer/COSXMLCopyTask.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.util.LinkedHashMap;
5454
import java.util.List;
5555
import java.util.Map;
56+
import java.util.UUID;
5657
import java.util.concurrent.atomic.AtomicBoolean;
5758
import java.util.concurrent.atomic.AtomicInteger;
5859

@@ -90,6 +91,10 @@ public final class COSXMLCopyTask extends COSXMLTask {
9091
private Object SYNC_UPLOAD_PART = new Object();
9192

9293
private HttpTaskMetrics httpTaskMetrics = new HttpTaskMetrics();
94+
/**
95+
* 标记一次请求的客户端唯一标识,用于日志上报和本地日志记录
96+
*/
97+
private String clientTraceId;
9398

9499
private LargeCopyStateListener largeCopyStateListenerHandler = new LargeCopyStateListener(){
95100
@Override
@@ -137,6 +142,7 @@ private void reportException(CosXmlRequest request, CosXmlClientException client
137142
this.bucket = bucket;
138143
this.cosPath = cosPath;
139144
this.copySourceStruct = copySourceStruct;
145+
this.clientTraceId = UUID.randomUUID().toString();
140146
}
141147

142148
COSXMLCopyTask( CosXmlSimpleService cosXmlService, CopyObjectRequest copyObjectRequest){
@@ -164,6 +170,7 @@ private void reportException(CosXmlRequest request, CosXmlClientException client
164170
* 复制操作
165171
*/
166172
protected void copy(){
173+
this.clientTraceId = UUID.randomUUID().toString();
167174
run();
168175
}
169176

@@ -178,6 +185,7 @@ private void smallFileCopy(){
178185
if(onSignatureListener != null){
179186
copyObjectRequest.setSign(onSignatureListener.onGetSign(copyObjectRequest));
180187
}
188+
copyObjectRequest.setClientTraceId(this.clientTraceId);
181189

182190
getHttpMetrics(copyObjectRequest, "CopyObjectRequest");
183191

@@ -229,6 +237,7 @@ private void initMultiUpload(CosXmlSimpleService cosXmlService){
229237
if(onSignatureListener != null){
230238
initMultipartUploadRequest.setSign(onSignatureListener.onGetSign(initMultipartUploadRequest));
231239
}
240+
initMultipartUploadRequest.setClientTraceId(this.clientTraceId);
232241

233242
getHttpMetrics(initMultipartUploadRequest, "InitMultipartUploadRequest");
234243

@@ -287,6 +296,7 @@ private void listMultiUpload(CosXmlSimpleService cosXmlService){
287296
if(onSignatureListener != null){
288297
listPartsRequest.setSign(onSignatureListener.onGetSign(listPartsRequest));
289298
}
299+
listPartsRequest.setClientTraceId(this.clientTraceId);
290300

291301
getHttpMetrics(listPartsRequest, "ListPartsRequest");
292302

@@ -348,6 +358,7 @@ private void uploadPartCopy(CosXmlSimpleService cosXmlService){
348358
if(onSignatureListener != null){
349359
uploadPartCopyRequest.setSign(onSignatureListener.onGetSign(uploadPartCopyRequest));
350360
}
361+
uploadPartCopyRequest.setClientTraceId(this.clientTraceId);
351362

352363
getHttpMetrics(uploadPartCopyRequest, "UploadPartCopyRequest");
353364

@@ -404,6 +415,7 @@ private void completeMultiUpload(CosXmlSimpleService cosXmlService){
404415
if(onSignatureListener != null){
405416
completeMultiUploadRequest.setSign(onSignatureListener.onGetSign(completeMultiUploadRequest));
406417
}
418+
completeMultiUploadRequest.setClientTraceId(this.clientTraceId);
407419

408420
getHttpMetrics(completeMultiUploadRequest, "CompleteMultiUploadRequest");
409421

@@ -481,6 +493,7 @@ private void abortMultiUpload(CosXmlSimpleService cosXmlService){
481493
if(onSignatureListener != null){
482494
abortMultiUploadRequest.setSign(onSignatureListener.onGetSign(abortMultiUploadRequest));
483495
}
496+
abortMultiUploadRequest.setClientTraceId(this.clientTraceId);
484497

485498
getHttpMetrics(abortMultiUploadRequest, "AbortMultiUploadRequest");
486499

@@ -601,6 +614,7 @@ public void onStateChanged(String taskId, int state) {
601614
updateState(TransferState.IN_PROGRESS, null, null, false);
602615
}
603616
});
617+
headObjectRequest.setClientTraceId(this.clientTraceId);
604618
cosXmlService.headObjectAsync(headObjectRequest, new CosXmlResultListener() {
605619
@Override
606620
public void onSuccess(CosXmlRequest request, CosXmlResult result) {

0 commit comments

Comments
 (0)