Search in sources :

Example 1 with AccessControlList

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

the class CreateBucketTest method testPutWithStorageTypeAndLocation.

@Test
public void testPutWithStorageTypeAndLocation() {
    final String bucketName = "bucket-with-storage-and-location";
    CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
    createBucketRequest.setStorageClass(StorageClass.Standard);
    createBucketRequest.setLocationConstraint(OSS_TEST_REGION);
    try {
        ossClient.createBucket(createBucketRequest);
        AccessControlList returnedAcl = ossClient.getBucketAcl(bucketName);
        Set<Grant> grants = returnedAcl.getGrants();
        Assert.assertEquals(0, grants.size());
        System.out.println(returnedAcl.toString());
        BucketList buckets = ossClient.listBuckets(bucketName, "", 100);
        Assert.assertEquals(1, buckets.getBucketList().size());
        Assert.assertEquals(StorageClass.Standard, buckets.getBucketList().get(0).getStorageClass());
        Assert.assertEquals(OSS_TEST_REGION, buckets.getBucketList().get(0).getLocation());
        Assert.assertEquals(buckets.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : AccessControlList(com.aliyun.oss.model.AccessControlList) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) Grant(com.aliyun.oss.model.Grant) CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) BucketList(com.aliyun.oss.model.BucketList) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 2 with AccessControlList

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

the class CreateBucketTest method testPutWithCannedACL.

@Test
public void testPutWithCannedACL() {
    final String bucketName = "bucket-with-canned-acl";
    CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
    try {
        // Create bucket with default(private) acl
        ossClient.createBucket(createBucketRequest);
        AccessControlList returnedAcl = ossClient.getBucketAcl(bucketName);
        Set<Grant> grants = returnedAcl.getGrants();
        Assert.assertEquals(0, grants.size());
        Assert.assertEquals(returnedAcl.getCannedACL(), CannedAccessControlList.Private);
        // Try to create existing bucket without setting acl
        ossClient.createBucket(bucketName);
        waitForCacheExpiration(5);
        returnedAcl = ossClient.getBucketAcl(bucketName);
        grants = returnedAcl.getGrants();
        Assert.assertEquals(0, grants.size());
        // Create bucket with public-read acl
        createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
        ossClient.createBucket(createBucketRequest);
        waitForCacheExpiration(5);
        returnedAcl = ossClient.getBucketAcl(bucketName);
        grants = returnedAcl.getGrants();
        Assert.assertEquals(1, grants.size());
        Grant grant = (Grant) grants.toArray()[0];
        Assert.assertEquals(GroupGrantee.AllUsers, grant.getGrantee());
        Assert.assertEquals(Permission.Read, grant.getPermission());
        Assert.assertEquals(returnedAcl.getCannedACL(), CannedAccessControlList.PublicRead);
        // Try to create existing bucket without setting acl
        ossClient.createBucket(bucketName);
        waitForCacheExpiration(5);
        returnedAcl = ossClient.getBucketAcl(bucketName);
        grants = returnedAcl.getGrants();
        Assert.assertEquals(1, grants.size());
        grant = (Grant) grants.toArray()[0];
        Assert.assertEquals(GroupGrantee.AllUsers, grant.getGrantee());
        Assert.assertEquals(Permission.Read, grant.getPermission());
        // Create bucket with public-read-write acl
        createBucketRequest.setCannedACL(CannedAccessControlList.PublicReadWrite);
        ossClient.createBucket(createBucketRequest);
        waitForCacheExpiration(5);
        returnedAcl = ossClient.getBucketAcl(bucketName);
        grants = returnedAcl.getGrants();
        Assert.assertEquals(1, grants.size());
        grant = (Grant) grants.toArray()[0];
        Assert.assertEquals(GroupGrantee.AllUsers, grant.getGrantee());
        Assert.assertEquals(Permission.FullControl, grant.getPermission());
        Assert.assertEquals(returnedAcl.getCannedACL(), CannedAccessControlList.PublicReadWrite);
        // Try to create existing bucket without setting acl
        ossClient.createBucket(bucketName);
        waitForCacheExpiration(5);
        returnedAcl = ossClient.getBucketAcl(bucketName);
        grants = returnedAcl.getGrants();
        Assert.assertEquals(1, grants.size());
        grant = (Grant) grants.toArray()[0];
        Assert.assertEquals(GroupGrantee.AllUsers, grant.getGrantee());
        Assert.assertEquals(Permission.FullControl, grant.getPermission());
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : AccessControlList(com.aliyun.oss.model.AccessControlList) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) Grant(com.aliyun.oss.model.Grant) CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 3 with AccessControlList

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

the class CreateBucketTest method testPutWithStorageTypeFunc.

@Ignore
public void testPutWithStorageTypeFunc() {
    final String bucketName = "bucket-with-storage-type-func";
    try {
        ossClient.createBucket(new CreateBucketRequest(bucketName).withStorageType(StorageClass.IA));
        AccessControlList returnedAcl = ossClient.getBucketAcl(bucketName);
        Set<Grant> grants = returnedAcl.getGrants();
        Assert.assertEquals(0, grants.size());
        BucketList buckets = ossClient.listBuckets(bucketName, "", 100);
        Assert.assertEquals(1, buckets.getBucketList().size());
        Assert.assertEquals(StorageClass.IA, buckets.getBucketList().get(0).getStorageClass());
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : AccessControlList(com.aliyun.oss.model.AccessControlList) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) Grant(com.aliyun.oss.model.Grant) CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) BucketList(com.aliyun.oss.model.BucketList) OSSException(com.aliyun.oss.OSSException) Ignore(org.junit.Ignore)

Example 4 with AccessControlList

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

the class BucketAclTest method testUnormalGetBucketAcl.

@Test
public void testUnormalGetBucketAcl() {
    // Get non-existent bucket
    final String nonexistentBucket = "unormal-get-bucket-acl";
    try {
        ossClient.getBucketAcl(nonexistentBucket);
        Assert.fail("Get bucket acl should not be successful");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_BUCKET_ERR));
    }
    // Get bucket without ownership
    final String bucketWithoutOwnership = "oss";
    try {
        ossClient.getBucketAcl(bucketWithoutOwnership);
        Assert.fail("Get bucket referer should not be successful");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.ACCESS_DENIED, e.getErrorCode());
    }
    // Get bucket using default acl
    final String bucketUsingDefaultAcl = "bucket-using-default-acl";
    try {
        ossClient.createBucket(bucketUsingDefaultAcl);
        AccessControlList returnedACL = ossClient.getBucketAcl(bucketUsingDefaultAcl);
        Set<Grant> grants = returnedACL.getGrants();
        // No grants when using default acl
        Assert.assertEquals(0, grants.size());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketUsingDefaultAcl);
    }
}
Also used : AccessControlList(com.aliyun.oss.model.AccessControlList) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) Grant(com.aliyun.oss.model.Grant) OSSException(com.aliyun.oss.OSSException) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 5 with AccessControlList

use of com.aliyun.oss.model.AccessControlList in project druid by druid-io.

the class OssTaskLogsTest method testPushInternal.

private List<Grant> testPushInternal(boolean disableAcl, String ownerId, String ownerDisplayName) throws Exception {
    EasyMock.expect(ossClient.putObject(EasyMock.anyObject())).andReturn(new PutObjectResult()).once();
    AccessControlList aclExpected = new AccessControlList();
    aclExpected.setOwner(new Owner(ownerId, ownerDisplayName));
    EasyMock.expect(ossClient.getBucketAcl(TEST_BUCKET)).andReturn(aclExpected).once();
    EasyMock.expect(ossClient.putObject(EasyMock.anyObject(PutObjectRequest.class))).andReturn(new PutObjectResult()).once();
    EasyMock.replay(ossClient);
    OssTaskLogsConfig config = new OssTaskLogsConfig();
    config.setDisableAcl(disableAcl);
    config.setBucket(TEST_BUCKET);
    CurrentTimeMillisSupplier timeSupplier = new CurrentTimeMillisSupplier();
    OssInputDataConfig inputDataConfig = new OssInputDataConfig();
    OssTaskLogs taskLogs = new OssTaskLogs(ossClient, config, inputDataConfig, timeSupplier);
    String taskId = "index_test-datasource_2019-06-18T13:30:28.887Z";
    File logFile = tempFolder.newFile("test_log_file");
    taskLogs.pushTaskLog(taskId, logFile);
    return new ArrayList<>(aclExpected.getGrants());
}
Also used : AccessControlList(com.aliyun.oss.model.AccessControlList) Owner(com.aliyun.oss.model.Owner) PutObjectResult(com.aliyun.oss.model.PutObjectResult) CurrentTimeMillisSupplier(org.apache.druid.common.utils.CurrentTimeMillisSupplier) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

AccessControlList (com.aliyun.oss.model.AccessControlList)11 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)9 OSSException (com.aliyun.oss.OSSException)7 Grant (com.aliyun.oss.model.Grant)7 Test (org.junit.Test)6 CreateBucketRequest (com.aliyun.oss.model.CreateBucketRequest)4 BucketList (com.aliyun.oss.model.BucketList)3 Owner (com.aliyun.oss.model.Owner)2 ArrayList (java.util.ArrayList)2 Ignore (org.junit.Ignore)2 OSSClient (com.aliyun.oss.OSSClient)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 BucketLoggingResult (com.aliyun.oss.model.BucketLoggingResult)1 BucketReferer (com.aliyun.oss.model.BucketReferer)1 LifecycleRule (com.aliyun.oss.model.LifecycleRule)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1 PutObjectResult (com.aliyun.oss.model.PutObjectResult)1 SetBucketCORSRequest (com.aliyun.oss.model.SetBucketCORSRequest)1 CORSRule (com.aliyun.oss.model.SetBucketCORSRequest.CORSRule)1 SetBucketLifecycleRequest (com.aliyun.oss.model.SetBucketLifecycleRequest)1