Search in sources :

Example 1 with Bucket

use of com.aliyun.oss.model.Bucket in project Resource by lovelifeming.

the class AliyunOSSClient method createBucketName.

/**
 * 创建Bucket存储空间
 *
 * @param bucketName 存储空间
 * @return
 */
public String createBucketName(String bucketName) {
    if (!ossClient.doesBucketExist(bucketName)) {
        // 创建存储空间
        Bucket bucket = ossClient.createBucket(bucketName);
        LOG.info("创建存储空间成功: " + bucketName);
        return bucket.getName();
    }
    return bucketName;
}
Also used : Bucket(com.aliyun.oss.model.Bucket)

Example 2 with Bucket

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

the class ResponseParsers method parseGetBucketInfo.

/**
 * Unmarshall get bucket info response body to bucket info.
 */
public static BucketInfo parseGetBucketInfo(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        Element bucketElem = root.getChild("Bucket");
        BucketInfo bucketInfo = new BucketInfo();
        // owner
        Bucket bucket = new Bucket();
        String id = bucketElem.getChild("Owner").getChildText("ID");
        String displayName = bucketElem.getChild("Owner").getChildText("DisplayName");
        Owner owner = new Owner(id, displayName);
        bucket.setOwner(owner);
        // bucket
        bucket.setName(bucketElem.getChildText("Name"));
        bucket.setLocation(bucketElem.getChildText("Location"));
        bucket.setExtranetEndpoint(bucketElem.getChildText("ExtranetEndpoint"));
        bucket.setIntranetEndpoint(bucketElem.getChildText("IntranetEndpoint"));
        bucket.setCreationDate(DateUtil.parseIso8601Date(bucketElem.getChildText("CreationDate")));
        if (bucketElem.getChild("StorageClass") != null) {
            bucket.setStorageClass(StorageClass.parse(bucketElem.getChildText("StorageClass")));
        }
        bucketInfo.setBucket(bucket);
        // acl
        String aclString = bucketElem.getChild("AccessControlList").getChildText("Grant");
        CannedAccessControlList acl = CannedAccessControlList.parse(aclString);
        bucketInfo.setCannedACL(acl);
        switch(acl) {
            case PublicRead:
                bucketInfo.grantPermission(GroupGrantee.AllUsers, Permission.Read);
                break;
            case PublicReadWrite:
                bucketInfo.grantPermission(GroupGrantee.AllUsers, Permission.FullControl);
                break;
            default:
                break;
        }
        return bucketInfo;
    } catch (JDOMParseException e) {
        throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
    } catch (Exception e) {
        throw new ResponseParseException(e.getMessage(), e);
    }
}
Also used : JDOMParseException(org.jdom.input.JDOMParseException) Owner(com.aliyun.oss.model.Owner) Bucket(com.aliyun.oss.model.Bucket) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) BucketInfo(com.aliyun.oss.model.BucketInfo) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 3 with Bucket

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

the class UploadPartTest method testDeleteAllBuckets.

@Ignore
public void testDeleteAllBuckets() {
    try {
        List<Bucket> returnedBuckets = ossClient.listBuckets();
        for (Bucket bkt : returnedBuckets) {
            String bktName = bkt.getName();
            String keyMarker = null;
            String uploadIdMarker = null;
            ListMultipartUploadsRequest listMultipartUploadsRequest = null;
            MultipartUploadListing multipartUploadListing = null;
            List<MultipartUpload> multipartUploads = null;
            do {
                listMultipartUploadsRequest = new ListMultipartUploadsRequest(bktName);
                listMultipartUploadsRequest.setKeyMarker(keyMarker);
                listMultipartUploadsRequest.setUploadIdMarker(uploadIdMarker);
                multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest);
                multipartUploads = multipartUploadListing.getMultipartUploads();
                for (MultipartUpload mu : multipartUploads) {
                    String key = mu.getKey();
                    String uploadId = mu.getUploadId();
                    ossClient.abortMultipartUpload(new AbortMultipartUploadRequest(bktName, key, uploadId));
                }
                keyMarker = multipartUploadListing.getKeyMarker();
                uploadIdMarker = multipartUploadListing.getUploadIdMarker();
            } while (multipartUploadListing != null && multipartUploadListing.isTruncated());
            deleteBucketWithObjects(ossClient, bktName);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : Bucket(com.aliyun.oss.model.Bucket) MultipartUploadListing(com.aliyun.oss.model.MultipartUploadListing) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) MultipartUpload(com.aliyun.oss.model.MultipartUpload) OSSException(com.aliyun.oss.OSSException) ListMultipartUploadsRequest(com.aliyun.oss.model.ListMultipartUploadsRequest) Ignore(org.junit.Ignore)

Example 4 with Bucket

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

the class CreateBucketTest method testPutTooManyBuckets.

@Ignore
public void testPutTooManyBuckets() {
    final String bucketNamePrefix = "too-many-buckets-";
    try {
        List<String> existingBuckets = new ArrayList<String>();
        List<Bucket> bucketListing = ossClient.listBuckets();
        for (Bucket bkt : bucketListing) {
            existingBuckets.add(bkt.getName());
        }
        int remaindingAllowed = MAX_BUCKETS_ALLOWED - existingBuckets.size();
        List<String> newlyBuckets = new ArrayList<String>();
        int i = 0;
        while (i < remaindingAllowed) {
            String bucketName = bucketNamePrefix + i;
            try {
                ossClient.createBucket(bucketName);
                newlyBuckets.add(bucketName);
                i++;
                String loc = ossClient.getBucketLocation(bucketName);
                Assert.assertEquals(OSS_TEST_REGION, loc);
                Thread.sleep(50);
            } catch (Exception e) {
                System.out.println(e.getMessage());
                continue;
            }
        }
        // Try to create (MAX_BUCKETS_ALLOWED +1)th bucket
        try {
            ossClient.createBucket(bucketNamePrefix + MAX_BUCKETS_ALLOWED);
            Assert.fail("Create bucket should not be successful.");
        } catch (OSSException oe) {
            Assert.assertEquals(OSSErrorCode.TOO_MANY_BUCKETS, oe.getErrorCode());
            Assert.assertTrue(oe.getMessage().startsWith(TOO_MANY_BUCKETS_ERR));
        } finally {
            for (String bkt : newlyBuckets) {
                try {
                    ossClient.deleteBucket(bkt);
                } catch (Exception e) {
                // Ignore the exception and continue to delete remainding undesired buckets
                }
            }
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : Bucket(com.aliyun.oss.model.Bucket) ArrayList(java.util.ArrayList) OSSException(com.aliyun.oss.OSSException) OSSException(com.aliyun.oss.OSSException) Ignore(org.junit.Ignore)

Example 5 with Bucket

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

the class CreateBucketTest method testPutWithDefaultLocation.

@Test
public void testPutWithDefaultLocation() {
    final String bucketName = "bucket-with-default-location";
    try {
        Bucket bucket = ossClient.createBucket(bucketName);
        String loc = ossClient.getBucketLocation(bucketName);
        Assert.assertEquals(OSS_TEST_REGION, loc);
        Assert.assertEquals(bucket.getRequestId().length(), REQUEST_ID_LEN);
        // Create bucket with the same name again.
        bucket = ossClient.createBucket(bucketName);
        loc = ossClient.getBucketLocation(bucketName);
        Assert.assertEquals(OSS_TEST_REGION, loc);
        Assert.assertEquals(bucket.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : Bucket(com.aliyun.oss.model.Bucket) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

Bucket (com.aliyun.oss.model.Bucket)15 BucketList (com.aliyun.oss.model.BucketList)6 OSSException (com.aliyun.oss.OSSException)5 ArrayList (java.util.ArrayList)5 ListBucketsRequest (com.aliyun.oss.model.ListBucketsRequest)4 Test (org.junit.Test)4 OSSClient (com.aliyun.oss.OSSClient)2 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)2 Owner (com.aliyun.oss.model.Owner)2 ParseException (java.text.ParseException)2 Element (org.jdom.Element)2 JDOMParseException (org.jdom.input.JDOMParseException)2 Ignore (org.junit.Ignore)2 ClientException (com.aliyun.oss.ClientException)1 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)1 ResponseMessage (com.aliyun.oss.common.comm.ResponseMessage)1 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)1 BucketInfo (com.aliyun.oss.model.BucketInfo)1