1+ /*
2+ * Copyright (c) 2010-2020 Tencent Cloud. All rights reserved.
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5+ * of this software and associated documentation files (the "Software"), to deal
6+ * in the Software without restriction, including without limitation the rights
7+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+ * copies of the Software, and to permit persons to whom the Software is
9+ * furnished to do so, subject to the following conditions:
10+ *
11+ * The above copyright notice and this permission notice shall be included in all
12+ * copies or substantial portions of the Software.
13+ *
14+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+ * SOFTWARE.
21+ */
22+
23+ package com .tencent .cos .xml .common ;
24+
25+ import android .os .Environment ;
26+ import android .util .Log ;
27+
28+ import androidx .test .ext .junit .runners .AndroidJUnit4 ;
29+
30+ import com .tencent .cos .xml .CosXmlServiceConfig ;
31+ import com .tencent .cos .xml .CosXmlSimpleService ;
32+ import com .tencent .cos .xml .core .ServiceFactory ;
33+ import com .tencent .cos .xml .core .TestConst ;
34+ import com .tencent .cos .xml .core .TestLocker ;
35+ import com .tencent .cos .xml .core .TestUtils ;
36+ import com .tencent .cos .xml .exception .CosXmlClientException ;
37+ import com .tencent .cos .xml .exception .CosXmlServiceException ;
38+ import com .tencent .cos .xml .listener .CosXmlResultListener ;
39+ import com .tencent .cos .xml .model .CosXmlRequest ;
40+ import com .tencent .cos .xml .model .CosXmlResult ;
41+ import com .tencent .cos .xml .model .PresignedUrlRequest ;
42+ import com .tencent .cos .xml .model .object .GetObjectRequest ;
43+ import com .tencent .cos .xml .transfer .COSXMLDownloadTask ;
44+ import com .tencent .cos .xml .transfer .TransferConfig ;
45+ import com .tencent .cos .xml .transfer .TransferManager ;
46+ import com .tencent .qcloud .core .logger .QCloudLogger ;
47+
48+ import org .junit .Test ;
49+ import org .junit .runner .RunWith ;
50+
51+ import java .io .File ;
52+ import java .io .FileOutputStream ;
53+ import java .io .InputStream ;
54+ import java .net .HttpURLConnection ;
55+ import java .net .URL ;
56+
57+ /**
58+ *
59+ * <p>
60+ * Created by jordanqin on 2020/12/29.
61+ * Copyright 2010-2020 Tencent Cloud. All Rights Reserved.
62+ */
63+ @ RunWith (AndroidJUnit4 .class )
64+ public class SpecialCharactersTest {
65+ private String bucket = "0-a-1253960454" ;
66+ private String cosKey = "sentences/Cambridge_IELTS_Series/0+Cambridge A16~IETLS_A16_T1_Part_2.mp3" ;
67+
68+ private CosXmlSimpleService getCosXmlService () {
69+ CosXmlServiceConfig cosXmlServiceConfig = new CosXmlServiceConfig .Builder ()
70+ .isHttps (true )
71+ .setDebuggable (true )
72+ .setConnectionTimeout (4000 )
73+ .setSocketTimeout (4000 )
74+ .setTransferThreadControl (false )
75+ .setUploadMaxThreadCount (10 )
76+ .setDownloadMaxThreadCount (36 )
77+ .setRegion ("ap-nanjing" )
78+ .builder ();
79+ return ServiceFactory .INSTANCE .newService (cosXmlServiceConfig );
80+ }
81+
82+ private TransferManager getTransferManager () {
83+ TransferConfig transferConfig = new TransferConfig .Builder ()
84+ .setDivisionForUpload (2 * 1024 * 1024 )
85+ .setSliceSizeForUpload (1024 * 1024 )
86+ .setVerifyCRC64 (true )
87+ .setSliceSizeForCopy (5242880 )
88+ .setDividsionForCopy (5242880 )
89+ .build ();
90+ Log .d (TestConst .UT_TAG , String .valueOf (transferConfig .getDivisionForCopy ()));
91+ return new TransferManager (getCosXmlService (), transferConfig );
92+ }
93+
94+ @ Test public void testPresignedDownload () {
95+ PresignedUrlRequest presignedUrlRequest = new PresignedUrlRequest (bucket , cosKey );
96+ presignedUrlRequest .setRequestMethod ("GET" );
97+ presignedUrlRequest .setSignKeyTime (3600 );
98+ presignedUrlRequest .addNoSignHeader ("Host" );
99+ try {
100+ String signUrl = getCosXmlService ().getPresignedURL (presignedUrlRequest );
101+ Log .i ("QCloudTest" , signUrl );
102+ new Thread (() -> {
103+ String localPath = Environment .getExternalStoragePublicDirectory (Environment .DIRECTORY_DOWNLOADS ).getAbsolutePath () + "/wechat.png" ;
104+ downloadFile (signUrl , localPath );
105+ }).start ();
106+ TestUtils .sleep (5000 );
107+ } catch (CosXmlClientException clientException ) {
108+ QCloudLogger .i ("QCloudTest" , clientException .getMessage ());
109+ clientException .printStackTrace ();
110+ }
111+ }
112+ public void downloadFile (String fileUrl , String localPath ) {
113+ int retryCount = 0 ;
114+ boolean success = false ;
115+ while (!success && retryCount < 3 ) {
116+ HttpURLConnection connection = null ;
117+ InputStream input = null ;
118+ FileOutputStream output = null ;
119+ try {
120+ URL url = new URL (fileUrl );
121+ connection = (HttpURLConnection ) url .openConnection ();
122+ connection .connect ();
123+ if (connection .getResponseCode () != HttpURLConnection .HTTP_OK ) {
124+ if (connection .getResponseCode () >= 500 ) {
125+ retryCount ++;
126+ continue ;
127+ } else {
128+ throw new RuntimeException ("Server returned HTTP " + connection .getResponseCode ()
129+ + " " + connection .getResponseMessage ());
130+ }
131+ }
132+ input = connection .getInputStream ();
133+ output = new FileOutputStream (localPath );
134+ byte data [] = new byte [4096 ];
135+ int count ;
136+ while ((count = input .read (data )) != -1 ) {
137+ output .write (data , 0 , count );
138+ }
139+ success = true ;
140+ } catch (Exception e ) {
141+ retryCount ++;
142+ } finally {
143+ try {
144+ if (output != null )
145+ output .close ();
146+ if (input != null )
147+ input .close ();
148+ } catch (Exception ignored ) {
149+ }
150+ if (connection != null )
151+ connection .disconnect ();
152+ }
153+ }
154+ if (!success ) {
155+ throw new RuntimeException ("Failed to download file after 3 attempts" );
156+ }
157+ }
158+
159+ @ Test public void testSmallDownload () {
160+ TransferManager transferManager = getTransferManager ();
161+ GetObjectRequest getObjectRequest = new GetObjectRequest (bucket ,
162+ cosKey ,
163+ TestUtils .localParentPath ());
164+ COSXMLDownloadTask downloadTask = transferManager .download (TestUtils .getContext (),
165+ getObjectRequest );
166+ final TestLocker testLocker = new TestLocker ();
167+ downloadTask .setCosXmlResultListener (new CosXmlResultListener () {
168+ @ Override
169+ public void onSuccess (CosXmlRequest request , CosXmlResult result ) {
170+ File file = new File (getObjectRequest .getDownloadPath ());
171+ TestUtils .print ("download file size:" +file .length ());
172+ testLocker .release ();
173+ }
174+
175+ @ Override
176+ public void onFail (CosXmlRequest request , CosXmlClientException clientException , CosXmlServiceException serviceException ) {
177+ TestUtils .printError (TestUtils .getCosExceptionMessage (clientException , serviceException ));
178+ testLocker .release ();
179+ }
180+ });
181+ testLocker .lock ();
182+ TestUtils .assertCOSXMLTaskSuccess (downloadTask );
183+ }
184+ }
0 commit comments