Search in sources :

Example 6 with CreateBucketRequest

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

the class CreateBucketTest method testModifyExistingBucketLocation.

@Test
public void testModifyExistingBucketLocation() {
    final String bucketName = "modify-existing-bucket-location";
    try {
        ossClient.createBucket(bucketName);
        String loc = ossClient.getBucketLocation(bucketName);
        Assert.assertEquals(OSS_TEST_REGION, loc);
        // Try to modify location of existing bucket
        CreateBucketRequest request = new CreateBucketRequest(bucketName);
        request.setLocationConstraint("oss-ap-southeast-1");
        ossClient.createBucket(request);
        Assert.fail("Create bucket should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_LOCATION_CONSTRAINT, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(INVALID_LOCATION_CONSTRAINT_ERR));
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 7 with CreateBucketRequest

use of com.aliyun.oss.model.CreateBucketRequest 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 8 with CreateBucketRequest

use of com.aliyun.oss.model.CreateBucketRequest 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 9 with CreateBucketRequest

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

the class CreateBucketTest method testPutWithWithStorageTypeUnsupportedLocation.

@Test
public void testPutWithWithStorageTypeUnsupportedLocation() {
    final String bucketName = "bucket-with-storage-unsupported-location";
    final String unsupportedLocation = "oss-cn-zhengzhou";
    CreateBucketRequest request = new CreateBucketRequest(bucketName);
    request.setStorageClass(StorageClass.Standard);
    request.setLocationConstraint(unsupportedLocation);
    try {
        ossClient.createBucket(request);
        Assert.fail("Create bucket should not be successful.");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_LOCATION_CONSTRAINT, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(INVALID_LOCATION_CONSTRAINT_ERR));
    }
}
Also used : CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 10 with CreateBucketRequest

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

the class ArchiveTest method testUnormalOutofRestore.

@Test
public void testUnormalOutofRestore() {
    String bucketName = "unnormal-restore-test-bucket";
    String key = "unnormal-restore-object.txt";
    String filePath = null;
    try {
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
        createBucketRequest.setStorageClass(StorageClass.Archive);
        ossClient.createBucket(createBucketRequest);
        filePath = genFixedLengthFile(1024);
        ossClient.putObject(bucketName, key, new File(filePath));
        try {
            ossClient.getObject(bucketName, key);
            Assert.fail("Restore object should not be successful");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.INVALID_OBJECT_STATE, e.getErrorCode());
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        removeFile(filePath);
        ossClient.deleteObject(bucketName, key);
        ossClient.deleteBucket(bucketName);
    }
}
Also used : CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) OSSException(com.aliyun.oss.OSSException) TestUtils.genFixedLengthFile(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile) TestUtils.removeFile(com.aliyun.oss.integrationtests.TestUtils.removeFile) File(java.io.File) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

CreateBucketRequest (com.aliyun.oss.model.CreateBucketRequest)17 OSSException (com.aliyun.oss.OSSException)15 Test (org.junit.Test)11 BucketList (com.aliyun.oss.model.BucketList)6 AccessControlList (com.aliyun.oss.model.AccessControlList)4 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)4 Grant (com.aliyun.oss.model.Grant)4 Ignore (org.junit.Ignore)4 TestUtils.genFixedLengthFile (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile)3 TestUtils.removeFile (com.aliyun.oss.integrationtests.TestUtils.removeFile)3 File (java.io.File)3 OSS (com.aliyun.oss.OSS)2 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)2 OSSObject (com.aliyun.oss.model.OSSObject)2 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)2 ClientException (com.aliyun.oss.ClientException)1 Bucket (com.aliyun.oss.model.Bucket)1 ListBucketsRequest (com.aliyun.oss.model.ListBucketsRequest)1 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)1 ObjectAcl (com.aliyun.oss.model.ObjectAcl)1