
# 列举并行文件系统(HarmonyOS SDK)
#### 功能说明
调用获取并行文件系统列表接口，可列举当前账号所有地域下符合指定条件的并行文件系统。返回结果以并行文件系统名的字典序排序。
#### 接口约束
- 您必须拥有obs:bucket:ListAllMyBuckets权限，才能获取并行文件系统列表。建议使用IAM进行授权，配置方式详见[使用IAM自定义策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0121.html)。

- OBS支持的Region与Endpoint的对应关系，详细信息请参见[地区与终端节点](https://developer.huaweicloud.com/endpoint?OBS)。
- 必须设置BucketType参数为BucketType.POSIX，才可以列举出当前用户的并行文件系统。
 
#### 方法定义
```
listBuckets(input: ListBucketsInput): Response<ListBucketsOutput>
```
表1请求参数列表 
| **参数名称** | **参数类型**                                        | **是否必选** | **描述**                                                                                                          |
|:---|:---|:---|:---|
| input    | [ListBucketsInput] | 必选       | **参数解释** **：** 获取并行文件系统列表接口入参，详情参考[ListBucketsInput]。 |
   
 表2ListBucketsInput 
| **参数名称**      | **参数类型**                                        | **是否必选** | **描述**                                                                                                                                                                                                                                                                                                                                                                         |
|:---|:---|:---|:---|
| QueryLocation | boolean                                         | 可选       | **参数解释：** 是否同时查询并行文件系统的区域位置。 **取值范围：** - true：同时查询并行文件系统的区域位置。  - false：不查询并行文件系统的区域位置。   **默认取值：** false |
| BucketType    | [BucketType] | 可选       | **参数解释：** 列举指定类型的桶。 **取值范围：** 桶类型取值可参见[BucketType]。 **默认取值：** 如果不赋值则默认获取存储桶和并行文件系统列表。                                                                                                  |
   
 表3BucketType 
| **常量名** | **原始值** | **说明**              |
|:---|:---|:---|
| OBJECT  | OBJECT  | 对应类型为对象存储桶。         |
| POSIX   | POSIX   | 对应类型为并行文件系统（POSIX）。 |
   
#### 返回结果
表4返回结果 
| **参数类型**                                                                                                                                                     | **描述**                                                                                        |
|:---|:---|
| [Response] 说明： 该接口返回一个Promise类型，需要使用Promise、async/await语法处理。 | **参数解释：** 接口返回信息，详见[Response]。 |
   
 表5Response 
| **参数名称**        | **参数类型**                                              | **描述**                                                                                                                                                                                                |
|:---|:---|:---|
| CommonMsg       | [ICommonMsg]        | **参数解释：** 接口调用完成后的公共信息，包含HTTP状态码，操作失败的错误码等，详见[ICommonMsg]。                                                                           |
| InterfaceResult | [ListBucketsOutput] | **参数解释：** 操作成功后的结果数据，详见[ListBucketsOutput]。 **约束限制：** 当Status大于300时为空。 |
   
 表6ICommonMsg 
| **参数名称**  | **参数类型** | **描述**                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|:---|
| Status    | number   | **参数解释：** OBS服务端返回的HTTP状态码。 **取值范围：** 状态码是一组从2xx（成功）到4xx或5xx（错误）的数字代码，状态码表示了请求响应的状态。完整的状态码列表请参见[状态码](https://support.huaweicloud.com/api-obs/obs_04_0114.html)。 |
| Code      | string   | **参数解释：** OBS服务端返回的错误码。                                                                                                                                                                                                                                                                                                  |
| Message   | string   | **参数解释：** OBS服务端返回的错误描述。                                                                                                                                                                                                                                                                                                |
| HostId    | string   | **参数解释：** OBS服务端返回的请求服务端ID。                                                                                                                                                                                                                                                                                             |
| RequestId | string   | **参数解释：** OBS服务端返回的请求ID。                                                                                                                                                                                                                                                                                                |
| Id2       | string   | **参数解释：** OBS服务端返回的请求ID2。                                                                                                                                                                                                                                                                                             |
| Indicator | string   | **参数解释：** OBS服务端返回的详细错误码。                                                                                                                                                                                                                                                                                              |
   
 表7ListBucketsOutput 
| **参数名称**  | **参数类型**                                        | **描述**                                                                                                                                                                   |
|:---|:---|:---|
| RequestId | string                                          | **参数解释：** OBS服务端返回的请求ID。 **默认取值：** 无                                           |
| Owner     | [Owner]         | **参数解释：** 桶的所有者。 **取值范围：** 桶所有者取值参见[Owner]。        |
| Buckets   | [Bucket]\[\] | **参数解释：** 桶信息列表。 **取值范围：** 桶信息取值参见[Bucket]。 |
   
 表8Owner 
| **参数名称**    | **参数类型** | **是否必选**  | **描述**                                                                                                                                                                                                                                                                                                                                                                                     |
|:---|:---|:---|:---|
| ID          | string   | 作为请求参数时必选 | **参数解释：** 所有者的账号ID，即domain_id。 **取值范围：** 如何获取账号ID请参见[如何获取账号ID和用户ID？(HarmonyOS SDK)](https://support.huaweicloud.com/sdk-harmony-devg-obs/obs_34_0605.html) **默认取值：** 无 |
| DisplayName | string   | 可选        | **参数解释：** 所有者的账号用户名。 **默认取值：** 无                                                                                                                                                                                                                           |
   
 表9Bucket 
| **参数名称**     | **参数类型** | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
|:---|:---|:---|
| Name         | string   | **参数解释** **：** 桶名。 **约束限制：** - 桶的名字需全局唯一，不能与已有的任何桶名称重复，包括其他用户创建的桶。  - 桶命名规则如下： - 3～63个字符，数字或字母开头，支持小写字母、数字、"-"、"."。  - 禁止使用类IP地址。  - 禁止以"-"或"."开头及结尾。  - 禁止两个"."相邻（如："my..bucket"）。  - 禁止"."和"-"相邻（如："my-.bucket"和"my.-bucket"）。     **默认取值：** 无 |
| CreationDate | string   | **参数解释** **：** 桶的创建时间。 **默认取值：** 无                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| Location     | string   | **参数解释** **：** 桶所在的区域。 **约束限制：** 该参数定义了桶将会被创建在哪个区域，如果使用的终端节点是obs.myhuaweicloud.com，可以不携带此参数；如果使用的终端节点不是obs.myhuaweicloud.com，则必须携带此参数。 **取值范围：** 当前有效的OBS区域和终端节点的更多信息，请参考[地区和终端节点](https://developer.huaweicloud.com/endpoint?OBS)。终端节点即调用API的请求地址，不同服务不同区域的终端节点不同，您可以向企业管理员获取区域和终端节点信息。 **默认取值：** 终端节点为obs.myhuaweicloud.com且用户未设定区域时，默认为华北-北京一（cn-north-1）。                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| BucketType   | string   | **参数解释** **：** 桶类型。 **取值范围：** - 对象桶：OBJECT。  - 并行文件系统：POSIX。   **默认取值：** 无                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
   
#### 代码示例
本示例用于列举并行文件系统列表。
```
// 引入依赖包
import ObsClient, { BucketType, ListBucketsInput } from '@obs/esdk-obs-harmony';
import { process } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
const processManager = new process.ProcessManager();
// 创建ObsClient实例
const obsClient = new ObsClient({
  // 推荐通过环境变量获取AKSK，这里也可以使用其他外部引入方式传入，如果使用硬编码可能会存在泄露风险
  // 您可以登录访问管理控制台获取访问密钥AK/SK，获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
  AccessKeyId: processManager.getEnvironmentVar("ACCESS_KEY_ID"),
  SecretAccessKey: processManager.getEnvironmentVar("SECRET_ACCESS_KEY"),
  // 【可选】如果使用临时AK/SK和SecurityToken访问OBS，同样建议您尽量避免使用硬编码，以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK，也可以使用其他外部引入方式传入
  // SecurityToken: processManager.getEnvironmentVar("SECURITY_TOKEN"),
  // Server填写Bucket对应的Endpoint, 这里以华北-北京四为例，其他地区请按实际情况填写
  Server: "https://obs.cn-north-4.myhuaweicloud.com",
});
async function listBuckets() {
  try {
    const params: ListBucketsInput = {
      // 指定并行文件系统列表中是否存在Location字段，此处以true为例，默认是false
      QueryLocation: true,
      // 指定桶类型，此处以为 BucketType.POSIX 为例，即获取所有并行文件系统列表，默认不设置则获取所有桶和并行文件系统列表
      BucketType: BucketType.POSIX,
    };
    // 列举并行文件系统列表
    const result = await obsClient.listBuckets(params);
    if (result.CommonMsg.Status < 300) {
      hilog.info(0x0001, 'OBS-Harmony', "List buckets successful!");
      hilog.info(0x0001, 'OBS-Harmony', "RequestId: %s", result.CommonMsg.RequestId);
      hilog.info(0x0001, 'OBS-Harmony', 'Owner:');
      hilog.info(0x0001, 'OBS-Harmony', 'ID: %s', result.InterfaceResult.Owner.ID);
      hilog.info(0x0001, 'OBS-Harmony', 'Name: %s', result.InterfaceResult.Owner.DisplayName);
      hilog.info(0x0001, 'OBS-Harmony', 'Buckets:');
      for (let i = 0; i < result.InterfaceResult.Buckets.length; i++) {
          const val = result.InterfaceResult.Buckets[i];
          hilog.info(0x0001, 'OBS-Harmony', "Bucket[%d]-Name:%s,CreationDate:%s,Location: %s",
            i, val.Name, val.CreationDate, val.Location);
      }
      return;
    }
    hilog.error(0x0001, 'OBS-Harmony', "An ObsError was found, which means your request sent to OBS was rejected with an error response.");
    hilog.error(0x0001, 'OBS-Harmony', "Status: %d", result.CommonMsg.Status);
    hilog.error(0x0001, 'OBS-Harmony', "Code: %s", result.CommonMsg.Code);
    hilog.error(0x0001, 'OBS-Harmony', "Message: %s", result.CommonMsg.Message);
    hilog.error(0x0001, 'OBS-Harmony', "RequestId: %s", result.CommonMsg.RequestId);
  } catch (error) {
    const err = error as BusinessError;
    hilog.error(0x0001, 'OBS-Harmony', 'An Exception occurred: code=%d, message: %s', err.code, err.message);
  }
}
listBuckets();
```
#### 相关链接
- 关于并行文件系统的更多说明，请参见[什么是并行文件系统](https://support.huaweicloud.com/usermanual-obs/obs_13_0007.html)。
- 获取并行文件系统列表过程中返回的错误码含义、问题原因及处理措施可参考[OBS错误码](https://support.huaweicloud.com/api-obs/obs_04_0115.html#section1)。
- 列举并行文件系统常见问题请参见[并行文件系统常见问题](https://support.huaweicloud.com/obs_faq/obs_13_0015.html)。
 
