Skip to content

Commit 1902067

Browse files
liyanzhang505huiguangjun
authored andcommitted
fix the issue that setting acl in the uploadFile method does not take effect.
1 parent 566bd7d commit 1902067

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/main/java/com/aliyun/oss/internal/OSSUploadOperation.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.aliyun.oss.event.ProgressEventType;
5151
import com.aliyun.oss.event.ProgressListener;
5252
import com.aliyun.oss.event.ProgressPublisher;
53+
import com.aliyun.oss.model.CannedAccessControlList;
5354
import com.aliyun.oss.model.CompleteMultipartUploadRequest;
5455
import com.aliyun.oss.model.CompleteMultipartUploadResult;
5556
import com.aliyun.oss.model.InitiateMultipartUploadRequest;
@@ -602,6 +603,15 @@ public int compare(PartETag p1, PartETag p2) {
602603
completeUploadRequest.setRequestPayer(payer);
603604
}
604605

606+
ObjectMetadata metadata = uploadFileRequest.getObjectMetadata();
607+
if (metadata != null) {
608+
String acl = (String) metadata.getRawMetadata().get(OSSHeaders.OSS_OBJECT_ACL);
609+
if (acl != null && !acl.equals("")) {
610+
CannedAccessControlList accessControlList = CannedAccessControlList.parse(acl);
611+
completeUploadRequest.setObjectACL(accessControlList);
612+
}
613+
}
614+
605615
completeUploadRequest.setCallback(uploadFileRequest.getCallback());
606616

607617
return completeMultipartUploadWrap(uploadCheckPoint, completeUploadRequest);

src/test/java/com/aliyun/oss/integrationtests/UploadFileTest.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,63 @@ public void run() {
372372
}
373373
}
374374

375-
}
375+
@Test
376+
public void testAcl() {
377+
String key = "test-upload-file-with-acl";
378+
379+
// Upload file without acl setting, returned acl will be DEFAULT.
380+
try {
381+
File file = createSampleFile(key, 1024 * 500);
382+
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key, file.getAbsolutePath(), (1024 * 100), 10);
383+
384+
UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
385+
Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
386+
Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
387+
388+
ObjectAcl acl = ossClient.getObjectAcl(bucketName, key);
389+
Assert.assertEquals(acl.getPermission(), ObjectPermission.Default);
390+
} catch (Throwable e) {
391+
e.printStackTrace();
392+
Assert.fail(e.getMessage());
393+
}
394+
395+
// Upload With PRIVATE acl setting, returned acl will be PRIVATE.
396+
try {
397+
File file = createSampleFile(key, 1024 * 500);
398+
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key, file.getAbsolutePath(), (1024 * 100), 10);
399+
ObjectMetadata metadata = new ObjectMetadata();
400+
metadata.setObjectAcl(CannedAccessControlList.Private);
401+
uploadFileRequest.setObjectMetadata(metadata);
402+
403+
UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
404+
Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
405+
Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
406+
407+
ObjectAcl acl = ossClient.getObjectAcl(bucketName, key);
408+
Assert.assertEquals(acl.getPermission(), ObjectPermission.Private);
409+
} catch (Throwable e) {
410+
e.printStackTrace();
411+
Assert.fail(e.getMessage());
412+
}
413+
414+
// Test set acl null.
415+
try {
416+
File file = createSampleFile(key, 1024 * 500);
417+
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key, file.getAbsolutePath(), (1024 * 100), 10);
418+
ObjectMetadata metadata = new ObjectMetadata();
419+
metadata.setObjectAcl(null);
420+
uploadFileRequest.setObjectMetadata(metadata);
421+
422+
UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
423+
Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
424+
Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
425+
426+
ObjectAcl acl = ossClient.getObjectAcl(bucketName, key);
427+
Assert.assertEquals(acl.getPermission(), ObjectPermission.Default);
428+
} catch (Throwable e) {
429+
e.printStackTrace();
430+
Assert.fail(e.getMessage());
431+
}
432+
}
433+
434+
}

0 commit comments

Comments
 (0)