详情页标题前

阿里云对象存储OSSJava列举存储空间-云淘科技

详情页1

存储空间(Bucket)是用来存储对象(Object)的容器。对象都隶属于存储空间。存储空间按照字母顺序排列。您可以列举当前账号所有地域下符合指定条件的存储空间。

注意事项

  • 本文以华东1(杭州)外网Endpoint为例。如果您希望通过与OSS同地域的其他阿里云产品访问OSS,请使用内网Endpoint。关于OSS支持的Region与Endpoint的对应关系,请参见访问域名和数据中心。
  • 本文以从环境变量读取访问凭证为例。如何配置访问凭证,请参见Java配置访问凭证。

  • 本文以OSS域名新建OSSClient为例。如果您希望通过自定义域名、STS等方式新建OSSClient,请参见新建OSSClient。
  • 要列举存储空间,您必须具有oss:ListBuckets权限。具体操作,请参见为RAM用户授权自定义的权限策略。

  • 如果有低频类型或归档类型的存储空间,请使用Java SDK 2.6.0及以上版本。

  • 以下代码仅支持列举当前阿里云账号下所有地域的存储空间,不支持列举指定地域的存储空间。列举结果与填写endpoint关联的地域无关。

列举所有存储空间

以下代码用于列举当前账号所有地域下的存储空间。

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.Bucket;

import java.util.List;

public class Demo {

    public static void main(String[] args) throws Exception {
        // 以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // 列举当前账号所有地域下的存储空间。
            List 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();
            }
        }
    }
}

列举资源组中的存储空间

以下代码用于列举当前账号指定资源组中的存储空间。

说明

一个阿里云账号包含一个默认资源组和多个自定义的资源组。如果在创建存储空间时未指定资源组ID,则创建的存储空间均属于默认资源组。

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        // 以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写资源组ID。如果未填写资源组ID,则列举默认资源组中的存储空间。
        String rsId = "rg-aek27tc****";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // 列举存储空间。
            ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
            // 列举当前账号指定资源组中的存储空间。
            listBucketsRequest.setResourceGroupId(rsId);
            BucketList bucketList = ossClient.listBuckets(listBucketsRequest);
            for (Bucket bucket : bucketList.getBucketList()) {
                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();
            }
        }
    }
}               

列举指定前缀的存储空间

以下代码用于列举当前账号所有地域下以example为前缀(prefix)的存储空间。

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.BucketList;
import com.aliyun.oss.model.ListBucketsRequest;

public class Demo {

    public static void main(String[] args) throws Exception {
        // 以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
            // 列举当前账号所有地域下指定前缀的存储空间。            
            listBucketsRequest.setPrefix("example");
            BucketList bucketList = ossClient.listBuckets(listBucketsRequest);
            for (Bucket bucket : bucketList.getBucketList()) {
                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();
            }
        }
    }
}

列举指定marker之后的存储空间

以下代码用于列举当前账号所有地域下名称的字母序排在examplebucket之后的存储空间。

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws com.aliyuncs.exceptions.ClientException {
        // 以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "examplebucket";
        // 设置每页列举200个存储空间。
        int maxKeys = 200;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // 列举当前账号所有地域下字母序排在指定marker之后的存储空间。
            String nextMarker = bucketName;
            BucketList bucketListing;

            do {
                bucketListing = ossClient.listBuckets(new ListBucketsRequest().withMarker(nextMarker).withMaxKeys(maxKeys));

                List sums = bucketListing.getBucketList();
                for (Bucket s : sums) {
                    System.out.println("	" + s.getName());
                }

                nextMarker = bucketListing.getNextMarker();

            } while (bucketListing.isTruncated());
        } 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();
            }
        }
    }
}

列举指定个数的存储空间

以下代码用于列举当前账号所有地域下的存储空间,并指定列举的最大个数为500。

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.BucketList;
import com.aliyun.oss.model.ListBucketsRequest;

public class Demo {

    public static void main(String[] args) throws Exception {
        // 以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            ListBucketsRequest listBucketsRequest = new ListBucketsRequest();           
            // 列举当前账号所有地域下的存储空间,限定此次列举存储空间的最大个数为500。默认值为100,最大值为1000。
            listBucketsRequest.setMaxKeys(500);
            BucketList bucketList = ossClient.listBuckets(listBucketsRequest);
            for (Bucket bucket : bucketList.getBucketList()) {
                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();
            }
        }
    }
}

相关文档

  • 关于列举存储空间的完整示例代码,请参见GitHub示例。

  • 关于列举存储空间的API接口说明,请参见ListBuckets(GetService)。

内容没看懂? 不太想学习?想快速解决? 有偿解决: 联系专家

阿里云企业补贴进行中: 马上申请

腾讯云限时活动1折起,即将结束: 马上收藏

同尘科技为腾讯云授权服务中心。

购买腾讯云产品享受折上折,更有现金返利:同意关联,立享优惠

转转请注明出处:https://www.yunxiaoer.com/158003.html

(0)
上一篇 2023年12月10日
下一篇 2023年12月10日
详情页2

相关推荐

  • 阿里云对象存储OSSRuby生命周期-云淘科技

    OSS支持设置Bucket生命周期(Lifecycle)规则,自动删除过期的文件(Object)和碎片,或将到期的文件转储为低频或归档存储类型,从而节省存储费用。本文介绍如何管理存储空间(Bucket)的生命周期规则。 设置生命周期规则 以下代码用于设置生命周期规则: require ‘aliyun/oss’ client = Aliyun::OSS::Cl…

    阿里云对象存储 2023年12月10日
  • 阿里云RDS数据库对象存储服务路径-云淘科技

    Ganos支持基于阿里云对象存储服务(OSS)、MinIO和HDFS的栅格数据的创建,导入与导出操作。 OSS文件路径 基于OSS的文件路径格式如下: oss://:@[]/bucket_name/path_to/file[:] 说明 各参数解释如下: 如果是具有SubSet的NetCDF,可以通过:方式指定。 access_id和secrect_key分别…

    阿里云数据库 2023年12月9日
  • 信息流广告,信息流部分建议宽度830px,只针对默认列表样式,顺序随机
  • 阿里云对象存储OSScat(输出文件内容)-云淘科技

    cat命令仅支持将存储空间(Bucket)内文件(Object)的内容输出到屏幕。 重要 本文各命令行示例均基于Linux 64位系统,其他系统请将命令开头的./ossutil64替换成对应的Binary名称。详情请参见命令行工具ossutil快速入门。 命令格式 ./ossutil64 cat oss://bucketname/objectname [–…

    阿里云对象存储 2023年12月10日
  • 阿里云大数据开发治理平台 DataWorks用户授权与管理-云淘科技

    使用数据建模DATABLAU功能时,在进行定义标准、数据建模、数据开发、部署运维等不同任务时,通常需不同角色的用户进行操作,对应用户需获取相应的权限。本文为您介绍制定标准、数据建模并应用部署过程中推荐的用户角色权限规划与授权操作指导。 角色规划 使用数据建模并应用至数据开发的过程中,通常需要进行定义标准、数据建模、数据开发、部署运维等任务,下图为您示例不同流…

  • 阿里云对象存储OSSPython异常处理-云淘科技

    OSS Python SDK异常(OssError)分为三类:ClientError、RequestError和ServerError,这些异常定义在oss2.exceptions子模块中。 异常的变量、类型及描述如下表所示: 变量 类型0 描述 status int 如果为ServerError异常,则status为HTTP状态码。 如果为ClientEr…

    阿里云对象存储 2023年12月10日

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
本站为广大会员提供阿里云、腾讯云、华为云、百度云等一线大厂的购买,续费优惠,保证底价,买贵退差。