获取桶列表(HarmonyOS SDK)
功能说明
OBS桶是对象的容器,您上传的文件都存放在桶中。调用获取桶列表接口,可列举当前账号所有地域下符合指定条件的桶。返回结果以桶名的字典序排序。
接口约束
- 您必须拥有obs:bucket:ListAllMyBuckets权限,才能获取桶列表。建议使用IAM进行授权,配置方式详见使用IAM自定义策略。
- OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
方法定义
1 | listBuckets(input: ListBucketsInput): Response<ListBucketsOutput> |
请求参数
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| input | 必选 | 参数解释: 获取桶列表接口入参,详情参考ListBucketsInput。 |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| QueryLocation | boolean | 可选 | 参数解释: 是否同时查询桶的区域位置。 取值范围:
默认取值: false |
| BucketType | 可选 | 参数解释: 列举指定类型的桶。 取值范围: 桶类型取值可参见BucketType。 默认取值: 如果不赋值则默认获取存储桶和并行文件系统列表。 |
返回结果
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| CommonMsg | 参数解释: 接口调用完成后的公共信息,包含HTTP状态码,操作失败的错误码等,详见ICommonMsg。 | |
| InterfaceResult | 参数解释: 操作成功后的结果数据,详见ListBucketsOutput。 约束限制: 当Status大于300时为空。 |
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| Status | number | 参数解释: OBS服务端返回的HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 |
| Code | string | 参数解释: OBS服务端返回的错误码。 |
| Message | string | 参数解释: OBS服务端返回的错误描述。 |
| HostId | string | 参数解释: OBS服务端返回的请求服务端ID。 |
| RequestId | string | 参数解释: OBS服务端返回的请求ID。 |
| Id2 | string | 参数解释: OBS服务端返回的请求ID2。 |
| Indicator | string | 参数解释: OBS服务端返回的详细错误码。 |
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| RequestId | string | 参数解释: OBS服务端返回的请求ID。 默认取值: 无 |
| Owner | 参数解释: 桶的所有者。 取值范围: 桶所有者取值参见Owner。 | |
| Buckets | Bucket[] | 参数解释: 桶信息列表。 取值范围: 桶信息取值参见Bucket。 |
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| ID | string | 作为请求参数时必选 | 参数解释: 所有者的账号ID,即domain_id。 取值范围: 如何获取账号ID请参见如何获取账号ID和用户ID?(HarmonyOS SDK) 默认取值: 无 |
| DisplayName | string | 可选 | 参数解释: 所有者的账号用户名。 默认取值: 无 |
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| Name | string | 参数解释: 桶名。 约束限制:
默认取值: 无 |
| CreationDate | string | 参数解释: 桶的创建时间。 默认取值: 无 |
| Location | string | 参数解释: 桶所在的区域。 约束限制: 该参数定义了桶将会被创建在哪个区域,如果使用的终端节点是obs.myhuaweicloud.com,可以不携带此参数;如果使用的终端节点不是obs.myhuaweicloud.com,则必须携带此参数。 取值范围: 当前有效的OBS区域和终端节点的更多信息,请参考地区和终端节点。终端节点即调用API的请求地址,不同服务不同区域的终端节点不同,您可以向企业管理员获取区域和终端节点信息。 默认取值: 终端节点为obs.myhuaweicloud.com且用户未设定区域时,默认为华北-北京一(cn-north-1)。 |
| BucketType | string | 参数解释: 桶类型。 取值范围:
默认取值: 无 |
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | // 引入依赖包 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, // 指定桶类型,此处以为 “OBJECT” 为例,即获取所有桶列表,默认不设置则获取所有桶和并行文件系统列表 BucketType: BucketType.OBJECT, }; // 列举桶列表 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(); |