创建Bucket(一)
-
定义常量值:endpoint(访问域名)
accessKeyId (ram账号访问秘钥id)
accessKeySecret(ram子账号秘钥密码) -
创建Bucket
createBucket(String bucketName, int storageClass, int dataRedundancyType, int cannedACL)
输入参数 bucketName:桶名称 storageClass:存储类型
dataRedundancyType:数据容灾类型
cannedACL:数据读写权限
我们除了第一个参数是用户自己定义的字符串外,其他三个参数都是该存储空间的配置属性
我们在传入时采用int型 具体取值如下
存储类型:1:标准 2:低频 3:归档 4:冷归档
容灾类型:0:默认无 1:有
读写权限:0:默认私有 1:公共读 2:公共读写
输出:出参:int 1:成功 0:失败
public int createBucket(String bucketName, int storageClass, int dataRedundancyType, int cannedACL){
// System.out.println(bucketName + " " + storageClass + " " + dataRedundancyType + " " + cannedACL);
//1,参数列表:bucketName:桶名称 storageClass:存储类型
// dataRedundancyType:数据容灾类型
// cannedACL:数据读写权限
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try{
if(ossClient.doesBucketExist(bucketName)){
System.out.println("您已创建Bucket:" + bucketName + "。");
return 0;
}else {
System.out.println("您的Bucket不存在,创建Bucket:" + bucketName + "。");
CreateBucketRequest bucketRequest = new CreateBucketRequest(bucketName);
switch (storageClass){
case 1:
bucketRequest.setStorageClass(StorageClass.Standard);
break;
case 2:
bucketRequest.setStorageClass(StorageClass.IA);
break;
case 3:
bucketRequest.setStorageClass(StorageClass.Archive);
case 4:
bucketRequest.setStorageClass(StorageClass.ColdArchive);
}
switch (dataRedundancyType){
case 0:
bucketRequest.setDataRedundancyType(DataRedundancyType.LRS);
break;
case 1:
bucketRequest.setDataRedundancyType(DataRedundancyType.ZRS);
break;
}
switch (cannedACL){
case 1:
bucketRequest.setCannedACL(CannedAccessControlList.Private);
break;
case 2:
bucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
case 3:
bucketRequest.setCannedACL(CannedAccessControlList.PublicReadWrite);
}
ossClient.createBucket(bucketRequest);
}
BucketInfo info = ossClient.getBucketInfo(bucketName);
System.out.println("Bucket" + bucketName + "的信息如下: ");
System.out.println("\t数据中心:" + info.getBucket().getLocation());
System.out.println("\t创建时间: " + info.getBucket().getCreationDate());
System.out.println("\t用户标志:" + info.getBucket().getOwner());
} catch (OSSException oe){
oe.printStackTrace();
return 0;
} catch (Exception e){
e.printStackTrace();
return 0;
} finally{
ossClient.shutdown();
}
return 1;
}
3.列举存储空间
listAllBucket( ):
不需要参数,直接返回所有的Bucket
出参:List
public List<Bucket> listAllBuckets(){
OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
List<Bucket> buckets = ossClient.listBuckets(<