Search in sources :

Example 1 with ListBucketsRequest

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

the class BucketInfoTest method testListBucketWithEndpoint.

@Test
public void testListBucketWithEndpoint() {
    try {
        ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
        listBucketsRequest.setPrefix(bucketName);
        listBucketsRequest.setMaxKeys(1);
        BucketList buckets = ossClient.listBuckets(listBucketsRequest);
        Assert.assertEquals(buckets.getBucketList().size(), 1);
        Assert.assertNotNull(buckets.getBucketList().get(0).getExtranetEndpoint());
        Assert.assertNotNull(buckets.getBucketList().get(0).getIntranetEndpoint());
        Assert.assertEquals(buckets.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ListBucketsRequest(com.aliyun.oss.model.ListBucketsRequest) BucketList(com.aliyun.oss.model.BucketList) Test(org.junit.Test)

Example 2 with ListBucketsRequest

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

the class BucketInfoTest method testListBucketWithBid.

@Test
public void testListBucketWithBid() {
    try {
        ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
        listBucketsRequest.setPrefix(bucketName);
        listBucketsRequest.setMaxKeys(1);
        listBucketsRequest.setBid("26842");
        BucketList buckets = ossClient.listBuckets(listBucketsRequest);
        Assert.assertEquals(buckets.getBucketList().size(), 1);
        Assert.assertEquals(buckets.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ListBucketsRequest(com.aliyun.oss.model.ListBucketsRequest) BucketList(com.aliyun.oss.model.BucketList) Test(org.junit.Test)

Example 3 with ListBucketsRequest

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

the class TestBase method listAllBuckets.

protected static List<String> listAllBuckets(OSSClient client, String bucketPrefix) {
    List<String> bkts = new ArrayList<String>();
    String nextMarker = null;
    BucketList bucketList = null;
    do {
        ListBucketsRequest listBucketsRequest = new ListBucketsRequest(bucketPrefix, nextMarker, ListBucketsRequest.MAX_RETURNED_KEYS);
        bucketList = client.listBuckets(listBucketsRequest);
        nextMarker = bucketList.getNextMarker();
        for (Bucket b : bucketList.getBucketList()) {
            bkts.add(b.getName());
        }
    } while (bucketList.isTruncated());
    return bkts;
}
Also used : Bucket(com.aliyun.oss.model.Bucket) ListBucketsRequest(com.aliyun.oss.model.ListBucketsRequest) ArrayList(java.util.ArrayList) BucketList(com.aliyun.oss.model.BucketList)

Example 4 with ListBucketsRequest

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

use of com.aliyun.oss.model.ListBucketsRequest 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)

Aggregations

ListBucketsRequest (com.aliyun.oss.model.ListBucketsRequest)7 BucketList (com.aliyun.oss.model.BucketList)5 Bucket (com.aliyun.oss.model.Bucket)4 Test (org.junit.Test)4 OSSException (com.aliyun.oss.OSSException)2 ArrayList (java.util.ArrayList)2 ClientException (com.aliyun.oss.ClientException)1 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 CreateBucketRequest (com.aliyun.oss.model.CreateBucketRequest)1 OSSObject (com.aliyun.oss.model.OSSObject)1 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)1 ObjectAcl (com.aliyun.oss.model.ObjectAcl)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)1