Search in sources :

Example 1 with SetObjectAclRequest

use of com.aliyun.oss.model.SetObjectAclRequest in project aliyun-oss-java-sdk by aliyun.

the class ObjectVersionTest method testObjectAcl.

@Test
public void testObjectAcl() {
    String key = "version-test-set-object-acl";
    long inputStreamLength = 1024;
    try {
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ObjectMetadata metadata = new ObjectMetadata();
        PutObjectResult putResult = ossClient.putObject(bucketName, key, instream, metadata);
        Assert.assertNotNull(putResult.getVersionId());
        String versionId1 = putResult.getVersionId();
        instream = genFixedLengthInputStream(inputStreamLength);
        putResult = ossClient.putObject(bucketName, key, instream, metadata);
        Assert.assertNotNull(putResult.getVersionId());
        String versionId2 = putResult.getVersionId();
        // Set version1 'PublicRead'
        SetObjectAclRequest setObjectAclRequest = new SetObjectAclRequest(bucketName, key, versionId1, CannedAccessControlList.PublicRead);
        ossClient.setObjectAcl(setObjectAclRequest);
        // Set version2 'Private'
        setObjectAclRequest = new SetObjectAclRequest(bucketName, key, versionId2).withCannedACL(CannedAccessControlList.Private);
        ossClient.setObjectAcl(setObjectAclRequest);
        // Check acl of object version1
        GenericRequest genericRequest = new GenericRequest(bucketName, key, versionId1);
        ObjectAcl objectAcl = ossClient.getObjectAcl(genericRequest);
        Assert.assertEquals(ObjectPermission.PublicRead, objectAcl.getPermission());
        Assert.assertNotNull(objectAcl.getVersionId());
        Assert.assertEquals(64, objectAcl.getVersionId().length());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : SetObjectAclRequest(com.aliyun.oss.model.SetObjectAclRequest) ObjectAcl(com.aliyun.oss.model.ObjectAcl) PutObjectResult(com.aliyun.oss.model.PutObjectResult) InputStream(java.io.InputStream) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GenericRequest(com.aliyun.oss.model.GenericRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 2 with SetObjectAclRequest

use of com.aliyun.oss.model.SetObjectAclRequest in project aliyun-oss-java-sdk by aliyun.

the class ObjectAclTest method testUnormalSetObjectAcl.

@Test
public void testUnormalSetObjectAcl() {
    try {
        // Set non-existent object
        final String nonexistentObject = "unormal-set-object-acl";
        try {
            SetObjectAclRequest request = new SetObjectAclRequest(bucketName, nonexistentObject).withCannedACL(CannedAccessControlList.Default);
            request.setCannedACL(CannedAccessControlList.Private);
            ossClient.setObjectAcl(request);
            Assert.fail("Set object acl should not be successful");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.NO_SUCH_KEY, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_KEY_ERR));
        }
        // Set unknown permission
        final String unknownPermission = "UnknownPermission";
        try {
            ObjectPermission permission = ObjectPermission.parsePermission(unknownPermission);
            Assert.assertEquals(ObjectPermission.Unknown, permission);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : SetObjectAclRequest(com.aliyun.oss.model.SetObjectAclRequest) OSSException(com.aliyun.oss.OSSException) ObjectPermission(com.aliyun.oss.model.ObjectPermission) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

OSSException (com.aliyun.oss.OSSException)2 SetObjectAclRequest (com.aliyun.oss.model.SetObjectAclRequest)2 Test (org.junit.Test)2 GenericRequest (com.aliyun.oss.model.GenericRequest)1 ObjectAcl (com.aliyun.oss.model.ObjectAcl)1 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)1 ObjectPermission (com.aliyun.oss.model.ObjectPermission)1 PutObjectResult (com.aliyun.oss.model.PutObjectResult)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1