Search in sources :

Example 11 with BucketList

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

the class ListBucketsTest method testUnormalListBuckets.

@Test
public void testUnormalListBuckets() {
    final String nonexistentBucketNamePrefix = "nonexistent-bucket-name-prefix-";
    try {
        // List all existing buckets prefix with 'nonexistent-bucket-name-prefix-'
        BucketList bucketList = ossClient.listBuckets(nonexistentBucketNamePrefix, null, null);
        Assert.assertEquals(0, bucketList.getBucketList().size());
        // Set 'max-keys' equal zero(MUST be between 1 and 1000)
        bucketList = ossClient.listBuckets(null, null, 0);
        Assert.fail("List bucket should not be successful");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
}
Also used : OSSException(com.aliyun.oss.OSSException) BucketList(com.aliyun.oss.model.BucketList) Test(org.junit.Test)

Example 12 with BucketList

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

the class ListBucketsTest method testNormalListBuckets.

@Test
public void testNormalListBuckets() {
    final String bucketNamePrefix = BUCKET_NAME_PREFIX + "normal-list-buckets-";
    try {
        List<Bucket> returnedBuckets = ossClient.listBuckets();
        for (Bucket bkt : returnedBuckets) {
            if (bkt.getName().startsWith(bucketNamePrefix)) {
                ossClient.deleteBucket(bkt.getName());
            }
        }
        waitForCacheExpiration(5);
        List<String> existingBuckets = new ArrayList<String>();
        returnedBuckets = ossClient.listBuckets();
        for (Bucket bkt : returnedBuckets) {
            if (bkt.getName().startsWith(bucketNamePrefix)) {
                existingBuckets.add(bkt.getName());
            }
        }
        int remaindingAllowed = MAX_BUCKETS_ALLOWED - existingBuckets.size();
        List<String> newlyBuckets = new ArrayList<String>();
        for (int i = 0; i < remaindingAllowed; i++) {
            String bucketName = bucketNamePrefix + i;
            try {
                ossClient.createBucket(bucketName);
                newlyBuckets.add(bucketName);
                waitForCacheExpiration(5);
                String loc = ossClient.getBucketLocation(bucketName);
                Assert.assertEquals(OSS_TEST_REGION, loc);
            } catch (Exception e) {
                Assert.fail(e.getMessage());
            }
        }
        waitForCacheExpiration(5);
        // List all existing buckets
        returnedBuckets = ossClient.listBuckets();
        existingBuckets.clear();
        for (Bucket bkt : returnedBuckets) {
            if (bkt.getName().startsWith(bucketNamePrefix)) {
                existingBuckets.add(bkt.getName());
            }
        }
        Assert.assertEquals(MAX_BUCKETS_ALLOWED, existingBuckets.size());
        // List all existing buckets prefix with 'normal-list-buckets-'
        BucketList bucketList = ossClient.listBuckets(bucketNamePrefix, null, null);
        Assert.assertEquals(remaindingAllowed, bucketList.getBucketList().size());
        for (Bucket bkt : bucketList.getBucketList()) {
            Assert.assertTrue(bkt.getName().startsWith(bucketNamePrefix));
        }
        // List 'max-keys' buckets each time
        final int maxKeys = 3;
        bucketList = ossClient.listBuckets(bucketNamePrefix, null, maxKeys);
        Assert.assertTrue(bucketList.getBucketList().size() <= 3);
        returnedBuckets.clear();
        returnedBuckets.addAll(bucketList.getBucketList());
        while (bucketList.isTruncated()) {
            bucketList = ossClient.listBuckets(new ListBucketsRequest(bucketNamePrefix, bucketList.getNextMarker(), maxKeys));
            Assert.assertTrue(bucketList.getBucketList().size() <= 3);
            returnedBuckets.addAll(bucketList.getBucketList());
        }
        Assert.assertEquals(remaindingAllowed, returnedBuckets.size());
        for (Bucket bkt : returnedBuckets) {
            Assert.assertTrue(bkt.getName().startsWith(bucketNamePrefix));
        }
        for (String bkt : newlyBuckets) {
            ossClient.deleteBucket(bkt);
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : Bucket(com.aliyun.oss.model.Bucket) ListBucketsRequest(com.aliyun.oss.model.ListBucketsRequest) ArrayList(java.util.ArrayList) BucketList(com.aliyun.oss.model.BucketList) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 13 with BucketList

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

the class OSSBucketOperation method listBuckets.

/**
 * List all my buckets.
 */
public List<Bucket> listBuckets() throws OSSException, ClientException {
    BucketList bucketList = listBuckets(new ListBucketsRequest(null, null, null));
    List<Bucket> buckets = bucketList.getBucketList();
    while (bucketList.isTruncated()) {
        bucketList = listBuckets(new ListBucketsRequest(null, bucketList.getNextMarker(), null));
        buckets.addAll(bucketList.getBucketList());
    }
    return buckets;
}
Also used : Bucket(com.aliyun.oss.model.Bucket) ListBucketsRequest(com.aliyun.oss.model.ListBucketsRequest) BucketList(com.aliyun.oss.model.BucketList)

Example 14 with BucketList

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

the class OSSResponseParserTest method testParseListBucket.

@Test
public void testParseListBucket() throws Exception {
    String filename = "listBucket.xml";
    InputStream in = getInputStream(filename);
    BucketList bucketList = ResponseParsers.parseListBucket(in);
    assertEquals(null, bucketList.getPrefix());
    assertEquals(null, bucketList.getMarker());
    assertEquals(null, bucketList.getMaxKeys());
    assertEquals(false, bucketList.isTruncated());
    assertEquals(null, bucketList.getNextMarker());
    List<Bucket> buckets = bucketList.getBucketList();
    Bucket bucket1 = buckets.get(0);
    Assert.assertEquals("51744", bucket1.getOwner().getId());
    Assert.assertEquals("51744", bucket1.getOwner().getDisplayName());
    Assert.assertEquals("pacjux7y1b86pmtu7g8d6b7z-test-bucket", bucket1.getName());
    Assert.assertEquals(DateUtil.parseIso8601Date("2012-02-09T01:49:38.000Z"), bucket1.getCreationDate());
    Bucket bucket2 = buckets.get(1);
    Assert.assertEquals("51744", bucket2.getOwner().getId());
    Assert.assertEquals("51744", bucket2.getOwner().getDisplayName());
    Assert.assertEquals("ganshumantest", bucket2.getName());
    Assert.assertEquals(DateUtil.parseIso8601Date("2012-02-09T06:38:47.000Z"), bucket2.getCreationDate());
    in.close();
    filename = "listBucketTruncated.xml";
    in = getInputStream(filename);
    bucketList = ResponseParsers.parseListBucket(in);
    assertEquals("asdasdasdasd", bucketList.getPrefix());
    assertEquals("asdasdasd", bucketList.getMarker());
    assertEquals(Integer.valueOf(1), bucketList.getMaxKeys());
    assertEquals(true, bucketList.isTruncated());
    assertEquals("asdasdasdasdasd", bucketList.getNextMarker());
    buckets = bucketList.getBucketList();
    bucket1 = buckets.get(0);
    Assert.assertEquals("51744", bucket1.getOwner().getId());
    Assert.assertEquals("51744", bucket1.getOwner().getDisplayName());
    Assert.assertEquals("asdasdasdasd", bucket1.getName());
    Assert.assertEquals("osslocation", bucket1.getLocation());
    Assert.assertEquals(DateUtil.parseIso8601Date("2014-05-15T11:18:32.000Z"), bucket1.getCreationDate());
    in.close();
}
Also used : Bucket(com.aliyun.oss.model.Bucket) InputStream(java.io.InputStream) BucketList(com.aliyun.oss.model.BucketList) Test(org.junit.Test)

Example 15 with BucketList

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

the class BucketListTest method testBucketList.

@Test
public void testBucketList() {
    BucketList bucketList = new BucketList();
    bucketList.setPrefix("prefix");
    bucketList.setMarker("marker");
    bucketList.setMaxKeys(Integer.valueOf(6));
    bucketList.setTruncated(true);
    bucketList.setNextMarker("nextMarker");
    List<Bucket> buckets = new ArrayList<Bucket>();
    Bucket bucket = new Bucket();
    bucket.setName("name");
    bucket.setLocation("osslocation");
    buckets.add(bucket);
    bucketList.setBucketList(buckets);
    assertEquals("prefix", bucketList.getPrefix());
    assertEquals("marker", bucketList.getMarker());
    assertEquals(6, bucketList.getMaxKeys().intValue());
    assertEquals("nextMarker", bucketList.getNextMarker());
    assertEquals(true, bucketList.isTruncated());
    assertEquals(1, bucketList.getBucketList().size());
    buckets = bucketList.getBucketList();
    bucket = buckets.get(0);
    assertEquals("name", bucket.getName());
    assertEquals("osslocation", bucket.getLocation());
}
Also used : Bucket(com.aliyun.oss.model.Bucket) ArrayList(java.util.ArrayList) BucketList(com.aliyun.oss.model.BucketList) Test(org.junit.Test)

Aggregations

BucketList (com.aliyun.oss.model.BucketList)16 Test (org.junit.Test)9 OSSException (com.aliyun.oss.OSSException)8 Bucket (com.aliyun.oss.model.Bucket)6 CreateBucketRequest (com.aliyun.oss.model.CreateBucketRequest)6 ListBucketsRequest (com.aliyun.oss.model.ListBucketsRequest)5 ArrayList (java.util.ArrayList)4 AccessControlList (com.aliyun.oss.model.AccessControlList)3 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)3 Grant (com.aliyun.oss.model.Grant)3 Ignore (org.junit.Ignore)3 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 Owner (com.aliyun.oss.model.Owner)1 InputStream (java.io.InputStream)1 ParseException (java.text.ParseException)1 Element (org.jdom.Element)1 JDOMParseException (org.jdom.input.JDOMParseException)1 BeforeClass (org.junit.BeforeClass)1