文档

列举云盒Bucket

更新时间:

云盒Bucket按字母序排列。您可以结合实际场景列举某个云盒ID下的所有云盒Bucket、指定前缀的云盒Bucket、指定个数的云盒Bucket等。

前提条件

  • 华东1(杭州)、华南1(深圳)、华南2(河源)、华北2(北京)、西南1(成都)地域支持使用OSS ON云盒服务。

  • 已购买云盒。具体操作,请参见购买云盒

列举条件

您可以通过设置prefix、marker或者max-keys参数列举满足指定条件的云盒Bucket。

参数名称

描述

prefix

限制返回的云盒Bucket名称必须以prefix作为前缀。如果不指定该参数,则返回所有云盒Bucket。

marker

限制结果从marker之后按字母排序的第一个开始返回。如果不指定该参数,则从头开始返回数据。

max-keys

限定此次返回云盒Bucket的最大个数。

取值范围:1~1000

默认值:100

操作步骤

使用阿里云SDK

仅支持通过Java SDK列举某个云盒ID下的所有云盒Bucket,Java SDK要求3.15.0及以上版本。

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;

import java.util.List;

public class Demo {

    public static void main(String[] args) throws Exception {
        // 填写云盒Endpoint。
        String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写云盒所在地域。
        String region = "cn-hangzhou";
        // 填写云盒ID。
        String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";


        // 创建OSSClient实例。
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
                .clientConfiguration(conf)
                .region(region)
                .cloudBoxId(cloudBoxId)
                .build();

        try {
            // 列举指定云盒ID下的所有云盒Bucket。
            List<Bucket> buckets = ossClient.listBuckets();
            for (Bucket bucket : buckets) {
                System.out.println(" - " + bucket.getName());
            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

使用命令行工具ossutil

关于使用ossutil列举云盒Bucket的具体操作,请参见ls

使用REST API

如果您的程序自定义要求较高,您可以直接发起REST API请求。直接发起REST API请求需要手动编写代码计算签名。更多信息,请参见ListBuckets(GetService)

  • 本页导读 (1)
文档反馈