11package com .qcloud .cos_migrate_tool .task ;
22
3+ import java .io .BufferedReader ;
4+ import java .io .File ;
5+ import java .io .FileInputStream ;
6+ import java .io .FileNotFoundException ;
7+ import java .io .IOException ;
8+ import java .io .InputStreamReader ;
39import java .util .List ;
410
511import org .apache .commons .codec .digest .DigestUtils ;
1521import com .qcloud .cos .model .ListObjectsRequest ;
1622import com .qcloud .cos .model .ObjectListing ;
1723import com .qcloud .cos .region .Region ;
24+ import com .qcloud .cos .utils .UrlEncoderUtils ;
1825import com .qcloud .cos_migrate_tool .config .CopyBucketConfig ;
1926import com .qcloud .cos_migrate_tool .config .MigrateType ;
2027import com .qcloud .cos_migrate_tool .meta .TaskStatics ;
@@ -27,6 +34,7 @@ public class MigrateCopyBucketTaskExecutor extends TaskExecutor {
2734 private String srcRegion ;
2835 private String srcBucketName ;
2936 private String srcCosPath ;
37+ private String srcFileList ;
3038
3139 public MigrateCopyBucketTaskExecutor (CopyBucketConfig config ) {
3240 super (MigrateType .MIGRATE_FROM_COS_BUCKET_COPY , config );
@@ -42,19 +50,20 @@ public MigrateCopyBucketTaskExecutor(CopyBucketConfig config) {
4250 clientConfig .setConnectionTimeout (5000 );
4351 clientConfig .setSocketTimeout (5000 );
4452
45- clientConfig .setUserAgent ("cos-migrate-tool-v1.0" );
53+ clientConfig .setUserAgent ("cos-migrate-tool-v1.3. 0" );
4654 this .srcCosClient = new COSClient (srcCred , clientConfig );
4755 this .srcRegion = config .getSrcRegion ();
4856 this .srcBucketName = config .getSrcBucket ();
4957 this .srcCosPath = config .getSrcCosPath ();
58+ this .srcFileList = config .getSrcFileList ();
5059 }
5160
5261 @ Override
5362 protected String buildTaskDbComment () {
5463 String comment = String .format (
55- "[time: %s], [destRegion: %s], [destBucketName: %s], [destCosFolder: %s], [srcRegion: %s], [srcBucketName: %s], [srcFolder: %s], [smallTaskExecutor: %d]\n " ,
64+ "[time: %s], [destRegion: %s], [destBucketName: %s], [destCosFolder: %s], [srcRegion: %s], [srcFileList:%s], [ srcBucketName: %s], [srcFolder: %s], [smallTaskExecutor: %d]\n " ,
5665 SystemUtils .getCurrentDateTime (), config .getRegion (), config .getBucketName (),
57- config .getCosPath (), srcRegion , srcBucketName , srcCosPath ,
66+ config .getCosPath (), srcRegion , srcFileList , srcBucketName , srcCosPath ,
5867 this .smallFileUploadExecutorNum );
5968 return comment ;
6069 }
@@ -73,49 +82,124 @@ protected String buildTaskDbFolderPath() {
7382
7483 @ Override
7584 public void buildTask () {
76- ListObjectsRequest listObjectsRequest =
77- new ListObjectsRequest (srcBucketName , srcCosPath , null , null , 1000 );
7885
7986 int lastDelimiter = srcCosPath .lastIndexOf ("/" );
80- ObjectListing objectListing ;
81- int retry_num = 0 ;
8287
83- do {
88+ if (!srcFileList .isEmpty ()) {
89+ File file = new File (srcFileList );
90+ if (!file .isFile () || !file .exists ()) {
91+ String printMsg = String .format ("file[%s] not exist or not file" , srcFileList );
92+ log .error (printMsg );
93+ System .out .println (printMsg );
94+ }
95+
96+ InputStreamReader read = null ;
8497 try {
85- while (true ) {
86- objectListing = srcCosClient .listObjects (listObjectsRequest );
87- List <COSObjectSummary > cosObjectSummaries = objectListing .getObjectSummaries ();
88- for (COSObjectSummary cosObjectSummary : cosObjectSummaries ) {
89- String srcKey = cosObjectSummary .getKey ();
90- String srcEtag = cosObjectSummary .getETag ();
91- long srcSize = cosObjectSummary .getSize ();
92- String keyName = srcKey .substring (lastDelimiter );
93- String copyDestKey = config .getCosPath () + keyName ;
94-
95- MigrateCopyBucketTask task =
96- new MigrateCopyBucketTask (semaphore , (CopyBucketConfig ) config ,
97- smallFileTransferManager , bigFileTransferManager , recordDb ,
98- srcCosClient , srcKey , srcSize , srcEtag , copyDestKey );
99- AddTask (task );
100- }
101- if (!objectListing .isTruncated ()) {
102- break ;
98+ read = new InputStreamReader (new FileInputStream (file ));
99+ } catch (FileNotFoundException e1 ) {
100+ e1 .printStackTrace ();
101+ return ;
102+ }
103+
104+ BufferedReader bufferedReader = new BufferedReader (read );
105+ String srcKey = null ;
106+
107+ try {
108+ while ((srcKey = bufferedReader .readLine ()) != null ){
109+
110+ srcKey = UrlEncoderUtils .urlDecode (srcKey );
111+
112+ String copyDestKey = null ;
113+ if (srcKey .startsWith ("/" )) {
114+ copyDestKey = config .getCosPath () + srcKey .substring (1 );
115+ srcKey = srcKey .substring (1 );
116+ } else {
117+ copyDestKey = config .getCosPath () + srcKey ;
103118 }
104- listObjectsRequest .setMarker (objectListing .getNextMarker ());
119+
120+ MigrateCopyBucketTask task = new MigrateCopyBucketTask (semaphore ,
121+ (CopyBucketConfig ) config , smallFileTransferManager ,
122+ bigFileTransferManager , recordDb , srcCosClient , srcKey , 0 ,
123+ "" , copyDestKey );
124+
125+ AddTask (task );
126+
105127 }
106-
128+
107129 TaskStatics .instance .setListFinished (true );
108130
109- return ;
110-
131+ } catch (IOException e ) {
132+ log .error (e .toString ());
133+ TaskStatics .instance .setListFinished (false );
134+ e .printStackTrace ();
111135 } catch (Exception e ) {
112- log .error ("List cos bucket occur a exception" , e );
136+ log .error (e .toString ());
137+ e .printStackTrace ();
113138 TaskStatics .instance .setListFinished (false );
114139 }
115140
116- ++retry_num ;
117-
118- } while (retry_num < 20 );
141+ try {
142+ bufferedReader .close ();
143+ } catch (IOException e ) {
144+ e .printStackTrace ();
145+ }
146+ try {
147+ read .close ();
148+ } catch (IOException e ) {
149+ e .printStackTrace ();
150+ }
151+
152+
153+ } else {
154+
155+ ListObjectsRequest listObjectsRequest =
156+ new ListObjectsRequest (srcBucketName , srcCosPath , null , null , 1000 );
157+
158+ ObjectListing objectListing ;
159+ int retry_num = 0 ;
160+
161+ do {
162+ try {
163+ while (true ) {
164+ objectListing = srcCosClient .listObjects (listObjectsRequest );
165+ List <COSObjectSummary > cosObjectSummaries =
166+ objectListing .getObjectSummaries ();
167+
168+ for (COSObjectSummary cosObjectSummary : cosObjectSummaries ) {
169+ String srcKey = cosObjectSummary .getKey ();
170+ String srcEtag = cosObjectSummary .getETag ();
171+ long srcSize = cosObjectSummary .getSize ();
172+ String keyName = srcKey .substring (lastDelimiter );
173+ String copyDestKey = config .getCosPath () + keyName ;
174+
175+ MigrateCopyBucketTask task = new MigrateCopyBucketTask (semaphore ,
176+ (CopyBucketConfig ) config , smallFileTransferManager ,
177+ bigFileTransferManager , recordDb , srcCosClient , srcKey , srcSize ,
178+ srcEtag , copyDestKey );
179+
180+ AddTask (task );
181+ }
182+
183+ if (!objectListing .isTruncated ()) {
184+ break ;
185+ }
186+
187+ listObjectsRequest .setMarker (objectListing .getNextMarker ());
188+ }
189+
190+ TaskStatics .instance .setListFinished (true );
191+
192+ return ;
193+
194+ } catch (Exception e ) {
195+ log .error ("List cos bucket occur a exception:{}" , e .toString ());
196+ TaskStatics .instance .setListFinished (false );
197+ }
198+
199+ ++retry_num ;
200+
201+ } while (retry_num < 20 );
202+ }
119203
120204 }
121205
0 commit comments