Skip to content

Commit 231c7e1

Browse files
committed
change csp acl copy
change csp acl copy
1 parent c9ded4f commit 231c7e1

File tree

7 files changed

+115
-10
lines changed

7 files changed

+115
-10
lines changed
321 KB
Binary file not shown.

src/main/java/com/qcloud/cos_migrate_tool/config/ConfigParser.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,13 @@ private boolean initCopyFromLocalConfig(Preferences prefs,
548548
copyLocalConfig.setLocalPath(localPathConfig);
549549

550550
String excludes = getConfigValue(prefs, LOCAL_SECTION_NAME, LOCAL_EXECLUDE);
551-
if (excludes != null && !excludes.trim().isEmpty()) {
551+
if (excludes != null && !excludes.trim().isEmpty()) {
552552
copyLocalConfig.setExcludes(excludes);
553+
} else {
554+
excludes = getConfigValue(prefs, LOCAL_SECTION_NAME, "excludes");
555+
if (excludes != null && !excludes.trim().isEmpty()) {
556+
copyLocalConfig.setExcludes(excludes);
557+
}
553558
}
554559

555560
String ignoreModifiedTimeLessThanStr =

src/main/java/com/qcloud/cos_migrate_tool/config/CopyBucketConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public String getSrcBucket() {
3333

3434
public void setSrcBucket(String srcBucket) {
3535
srcBucket = srcBucket.trim();
36-
String parrtern = ".*-(125|100|20)[0-9]{3,}$";
36+
String parrtern = ".*-[0-9]{3,}$";
3737
if (!Pattern.matches(parrtern, srcBucket)) {
3838
throw new IllegalArgumentException(
3939
"SrcBucketName must contain appid. example: test-1250001000");

src/main/java/com/qcloud/cos_migrate_tool/task/MigrateCspTask.java

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44
import java.net.URL;
55
import java.util.Date;
6+
import java.util.List;
67
import java.util.Map;
78
import java.util.UUID;
89
import java.util.concurrent.Semaphore;
@@ -15,13 +16,16 @@
1516
import com.qcloud.cos.model.AccessControlList;
1617
import com.qcloud.cos.model.GeneratePresignedUrlRequest;
1718
import com.qcloud.cos.model.GetObjectRequest;
19+
import com.qcloud.cos.model.Grant;
1820
import com.qcloud.cos.model.ObjectMetadata;
21+
import com.qcloud.cos.model.Permission;
1922
import com.qcloud.cos.transfer.TransferManager;
2023
import com.qcloud.cos_migrate_tool.config.CopyFromCspConfig;
2124
import com.qcloud.cos_migrate_tool.config.MigrateType;
2225
import com.qcloud.cos_migrate_tool.meta.TaskStatics;
2326
import com.qcloud.cos_migrate_tool.record.MigrateCompetitorRecordElement;
2427
import com.qcloud.cos_migrate_tool.record.RecordDb;
28+
import com.qcloud.cos_migrate_tool.utils.TceGrantee;
2529

2630
public 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) {

src/main/java/com/qcloud/cos_migrate_tool/task/MigrateCspTaskExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ public void buildTask() {
125125

126126

127127
}
128-
128+
129+
TaskStatics.instance.setListFinished(true);
129130
inputStream.close();
130131
bufferedReader.close();
131132
} catch (IOException e) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.qcloud.cos_migrate_tool.utils;
2+
3+
import com.qcloud.cos.endpoint.EndpointBuilder;
4+
import com.qcloud.cos.internal.BucketNameUtils;
5+
import com.qcloud.cos.internal.UrlComponentUtils;
6+
7+
public class PathStyleEndpointBuilder implements EndpointBuilder {
8+
private String endpointSuffix;
9+
public PathStyleEndpointBuilder(String endpointSuffix) {
10+
super();
11+
if (endpointSuffix == null) {
12+
throw new IllegalArgumentException("endpointSuffix must not be null");
13+
}
14+
while(endpointSuffix.startsWith(".")) {
15+
endpointSuffix = endpointSuffix.substring(1);
16+
}
17+
UrlComponentUtils.validateEndPointSuffix(endpointSuffix);
18+
this.endpointSuffix = endpointSuffix.trim();
19+
}
20+
21+
public String buildGeneralApiEndpoint(String bucketName) {
22+
BucketNameUtils.validateBucketName(bucketName);
23+
return String.format("%s", this.endpointSuffix);
24+
}
25+
26+
public String buildGetServiceApiEndpoint() {
27+
return this.endpointSuffix;
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.qcloud.cos_migrate_tool.utils;
2+
3+
import com.qcloud.cos.model.Grantee;
4+
5+
public class TceGrantee implements Grantee {
6+
7+
public String type = "";
8+
public String id = "";
9+
10+
public String getTypeIdentifier() {
11+
return type;
12+
}
13+
14+
public void setIdentifier(String id) {
15+
this.id = id;
16+
}
17+
18+
public void setTypeIdentifier(String type) {
19+
this.type = type;
20+
}
21+
22+
public String getIdentifier() {
23+
return id;
24+
}
25+
}

0 commit comments

Comments
 (0)