33import java .io .File ;
44import java .net .URL ;
55import java .util .Date ;
6+ import java .util .List ;
67import java .util .Map ;
78import java .util .UUID ;
89import java .util .concurrent .Semaphore ;
1516import com .qcloud .cos .model .AccessControlList ;
1617import com .qcloud .cos .model .GeneratePresignedUrlRequest ;
1718import com .qcloud .cos .model .GetObjectRequest ;
19+ import com .qcloud .cos .model .Grant ;
1820import com .qcloud .cos .model .ObjectMetadata ;
21+ import com .qcloud .cos .model .Permission ;
1922import com .qcloud .cos .transfer .TransferManager ;
2023import com .qcloud .cos_migrate_tool .config .CopyFromCspConfig ;
2124import com .qcloud .cos_migrate_tool .config .MigrateType ;
2225import com .qcloud .cos_migrate_tool .meta .TaskStatics ;
2326import com .qcloud .cos_migrate_tool .record .MigrateCompetitorRecordElement ;
2427import com .qcloud .cos_migrate_tool .record .RecordDb ;
28+ import com .qcloud .cos_migrate_tool .utils .TceGrantee ;
2529
2630public class MigrateCspTask extends Task {
2731
@@ -40,7 +44,7 @@ public MigrateCspTask(CopyFromCspConfig config, COSClient cosClient, String srcK
4044 this .etag = etag ;
4145 if (srcKey .startsWith ("/" )) {
4246 this .srcKey = srcKey .substring (1 );
43- }
47+ }
4448 }
4549
4650 private String buildCOSPath () {
@@ -128,15 +132,16 @@ public void doTask() {
128132
129133 String cosPath = buildCOSPath ();
130134 ObjectMetadata srcMetaData = null ;
131-
135+
132136 if (this .etag .isEmpty ()) {
133137 try {
134- srcMetaData = cosClient .getObjectMetadata (((CopyFromCspConfig )config ).getSrcBucket (), srcKey );
138+ srcMetaData = cosClient
139+ .getObjectMetadata (((CopyFromCspConfig ) config ).getSrcBucket (), srcKey );
135140 this .etag = srcMetaData .getETag ();
136141 this .fileSize = srcMetaData .getContentLength ();
137142 } catch (Exception e ) {
138- log .error ("[fail] [taskInfo: {}] [Head Obj Exception occur, msg {}]" ,
139- srcKey , e .getMessage ().toString ());
143+ log .error ("[fail] [taskInfo: {}] [Head Obj Exception occur, msg {}]" , srcKey ,
144+ e .getMessage ().toString ());
140145 TaskStatics .instance .addFailCnt ();
141146 return ;
142147 }
@@ -161,6 +166,7 @@ public void doTask() {
161166 .<GetObjectRequest >withGeneralProgressListener (
162167 getObjectProgressListener ),
163168 localFile );
169+
164170 if (!localFile .exists ()) {
165171 String printMsg =
166172 String .format ("[fail] [task_info: %s]" , cspRecordElement .buildKey ());
@@ -193,7 +199,7 @@ public void doTask() {
193199 return ;
194200 }
195201
196-
202+
197203 AccessControlList acl = null ;
198204 try {
199205 acl = cosClient .getObjectAcl (((CopyFromCspConfig ) config ).getSrcBucket (), srcKey );
@@ -207,6 +213,45 @@ public void doTask() {
207213 return ;
208214 }
209215
216+ // acl转换
217+ AccessControlList acl2 = new AccessControlList ();
218+
219+ if (acl != null ) {
220+
221+ List <Grant > grantList = acl .getGrantsAsList ();
222+
223+ for (int i = 0 ; i < grantList .size (); ++i ) {
224+ if (grantList .get (i ).getPermission () == Permission .Write ) {
225+ continue ;
226+ }
227+
228+ //System.out.printf("%s %s %s\n",srcKey, grantList.get(i).getGrantee().getTypeIdentifier(), grantList.get(i).getGrantee().getIdentifier());
229+ TceGrantee grantee = new TceGrantee ();
230+ if (grantList .get (i ).getGrantee ().getTypeIdentifier ().equalsIgnoreCase ("uri" )) {
231+
232+
233+ if (grantList .get (i ).getGrantee ().getIdentifier ()
234+ .equalsIgnoreCase ("http://cam.qcloud.com/groups/global/AllUsers" )) {
235+
236+
237+ grantee .setIdentifier ("qcs::cam::anonymous:anonymous" );
238+
239+ } else if (grantList .get (i ).getGrantee ().getIdentifier ().equalsIgnoreCase (
240+ "http://cam.qcloud.com/groups/global/AuthenticatedUsers" )) {
241+ grantee .setIdentifier ("authenticated" );
242+ }
243+
244+ grantee .setTypeIdentifier ("id" );
245+
246+ } else {
247+ grantee .setTypeIdentifier (grantList .get (i ).getGrantee ().getTypeIdentifier ());
248+ grantee .setIdentifier (grantList .get (i ).getGrantee ().getIdentifier ());
249+ }
250+
251+ acl2 .grantPermission (grantee , grantList .get (i ).getPermission ());
252+ }
253+ }
254+
210255
211256 // upload file
212257 try {
@@ -235,7 +280,7 @@ public void doTask() {
235280 }
236281
237282 String requestId = uploadFile (config .getBucketName (), cosPath , localFile ,
238- config .getStorageClass (), config .isEntireFileMd5Attached (), cosMetadata , acl );
283+ config .getStorageClass (), config .isEntireFileMd5Attached (), cosMetadata , acl2 );
239284 saveRecord (cspRecordElement );
240285 saveRequestId (cosPath , requestId );
241286 if (this .query_result == RecordDb .QUERY_RESULT .KEY_NOT_EXIST ) {
0 commit comments