列举存储桶

更新时间: 2020-03-24

目录

简单列举存储桶

分页列举存储桶详情

本节介绍如何列举存储桶。

  • 简单列举存储桶:主要用于获取存储桶名字、创建日期和用户信息。
  • 列举存储桶详情:支持前缀、分页查询,在简单列举存储桶的返回结果的基础上增加了存储类(storageClass)、访问权限(acl)、区域信息(location)、外网域名(extranetEndpoint)、内网域名(intranetEndpoint)。

简单列举存储桶

以下代码用于简单列举当前用户所有的存储桶,主要用于获取桶名:

// Endpoint以华北三为例,其它Region请按实际情况填写
String endpoint = "oss.cn-north-3.inspurcloudoss.com";
String accessKey = "<yourAccessKey>";
String secretKey = "<yourSecretKey>";

//创建OSSClient实例
OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);

//列举所有bucket
List<SimpleBucketInfo> simpleBucketInfos = ossClient.listAllBucket();
for (SimpleBucketInfo simpleBucketInfo : simpleBucketInfos) {
    System.out.print("\t" + simpleBucketInfo.getBucketName());
}

分页列举存储桶详情

以下代码用于分页列举存储桶详情,主要用于获取用户下所有存储桶的详情(storageClass、acl、location、extranetEndpoint、intranetEndpoint等):

// Endpoint以华北三为例,其它Region请按实际情况填写
String endpoint = "oss.cn-north-3.inspurcloudoss.com";
String accessKey = "<yourAccessKey>";
String secretKey = "<yourSecretKey>";

//创建OSSClient实例
OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);

ListAllBucketInfoRequest request = new ListAllBucketInfoRequest();
//设置过滤关键字
request.setFilterKey("filterString");
//设置获取的是第几页
request.setPageNo("1");
//设置获取的数量
request.setPageSize("20");
BucketInfoListWithPage bucketInfoListWithPage = ossClient.listAllBucketInfo(request);
for (BucketInfo bucketInfo : bucketInfoListWithPage.getBucketInfo()) {
System.out.print("bucket name: " + bucketInfo.getBucketName() + "\t");
System.out.print("bucket acl: " + bucketInfo.getAcl() + "\t");
System.out.print("bucket extranetEndpoint: " + bucketInfo.getExtranetEndpoint() + "\t");
System.out.print("bucket intranetEndpoint: " + bucketInfo.getIntranetEndpoint() + "\t");
System.out.print("bucket location: " + bucketInfo.getLocation() + "\t");
System.out.print("bucket storageClass: " + bucketInfo.getStorageClass() + "\t");
System.out.println("bucket creationDate: " + bucketInfo.getCreationDate());
}