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;
}
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);
}
}
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());
}
}
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());
}
}
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);
}
}
Aggregations