
# 设置桶的自定义域名(HarmonyOS SDK)
#### 功能说明
调用设置桶的自定义域名配置接口，您可以您可以为指定桶设置的自定义域名信息。
#### 接口约束
- 您必须是桶拥有者或拥有设置桶的自定义域名的权限，才能设置桶的自定义域名信息。建议使用IAM或桶策略进行授权，如果使用IAM则需授予obs:bucket:PutBucketCustomDomainConfiguration权限，如果使用桶策略则需授予PutBucketCustomDomainConfiguration权限。相关授权方式介绍可参见[OBS权限控制概述](https://support.huaweicloud.com/perms-cfg-obs/obs_40_0001.html)，配置方式详见[使用IAM自定义策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0121.html)、[自定义创建桶策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0123.html)。
- OBS支持的Region与Endpoint的对应关系，详细信息请参见[地区与终端节点](https://developer.huaweicloud.com/endpoint?OBS)。
 
#### 方法定义
```
setBucketCustomDomain(input: SetBucketCustomDomainInput): Response<BaseResponseOutput>
```
#### 请求参数
表1请求参数列表 
| **参数名称** | **参数类型**                                                 | **是否必选** | **描述**                                                                                                               |
|:---|:---|:---|:---|
| input    | [SetBucketCustomDomainInput] | 必选       | **参数解释：** 设置桶的自定义域名配置接口入参，详见[SetBucketCustomDomainInput]。 |
   
 表2SetBucketCustomDomainInput 
| **参数名称**   | **参数类型** | **是否可选** | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|:---|
| Bucket     | string   | 必选       | 桶名。 **约束限制：** - 桶的名字需全局唯一，不能与已有的任何桶名称重复，包括其他用户创建的桶。  - 桶命名规则如下： - 3～63个字符，数字或字母开头，支持小写字母、数字、"-"、"."。  - 禁止使用IP地址。  - 禁止以"-"或"."开头及结尾。  - 禁止两个"."相邻（如："my..bucket"）。  - 禁止"."和"-"相邻（如："my-.bucket"和"my.-bucket"）。    - 同一用户在同一个区域多次创建同名桶不会报错，创建的桶属性以第一次请求为准。   **取值范围：** 长度为3～63个字符。 |
| DomainName | string   | 必选       | **参数解释**： 需要设置的自定义域名。 **约束限制：** 必须满足域名规则。 **约束限制：** 最长256字节。 **默认取值**： 无。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
   
#### 返回结果
表3返回结果 
| **参数类型**                                                                                                                                                     | **描述**                                                                                        |
|:---|:---|
| [Response] 说明： 该接口返回一个Promise类型，需要使用Promise、async/await语法处理。 | **参数解释：** 接口返回信息，详见[Response]。 |
   
 表4Response 
| **参数名称**        | **参数类型**                                          | **描述**                                                                                                                                                                                          |
|:---|:---|:---|
| CommonMsg       | [ICommonMsg]    | **参数解释：** 接口调用完成后的公共信息，包含HTTP状态码，操作失败的错误码等，详见[ICommonMsg]。                                                                        |
| InterfaceResult | [BaseResponseOutput] | **参数解释：** 操作成功后的结果数据，详见[BaseResponseOutput]。 **约束限制：** 当Status大于300时为空。 |
   
 表5ICommonMsg 
| **参数名称**  | **参数类型** | **描述**                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|:---|
| 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服务端返回的详细错误码。                                                                                                                                                                                                                                                                                              |
   
 表6BaseResponseOutput 
| **参数名称**  | **参数类型** | **描述**                                                                                       |
|:---|:---|:---|
| RequestId | string   | **参数解释：** OBS服务端返回的请求ID。    |
| Id2       | string   | **参数解释：** OBS服务端返回的请求ID2。 |
   
#### 代码示例
本示例用于设置名为examplebucket桶的自定义域名。
```
// 引入依赖包
import ObsClient, { SetBucketCustomDomainInput } 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 setBucketCustomDomain() {
  try {
    const params: SetBucketCustomDomainInput = {
      // 指定存储桶名称
      Bucket: "examplebucket",
      // 指定桶的自定义域名，此处以www.example.com为例
      DomainName: "www.example.com"
    };
    // 设置桶的自定义域名
    const result = await obsClient.setBucketCustomDomain(params);
    if (result.CommonMsg.Status < 300) {
      hilog.info(0x0001, 'OBS-Harmony', "Set bucket(%s)'s customdomain successful!", params.Bucket);
      hilog.info(0x0001, 'OBS-Harmony', "RequestId: %s", result.CommonMsg.RequestId);
      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);
  }
}
setBucketCustomDomain();
```
#### 相关链接
- 关于设置桶的自定义域名的API说明，请参见[设置桶的自定义域名](https://support.huaweicloud.com/api-obs/obs_04_0059.html)。
- 设置桶的自定义域名过程中返回的错误码含义、问题原因及处理措施可参考[OBS错误码](https://support.huaweicloud.com/api-obs/obs_04_0115.html#section1)。
 
