设置镜像回源规则(Node.js SDK)
功能介绍
OBS提供数据回源功能,可以在被请求的数据不存在时,通过回源规则从源站获取对应数据。
接口约束
| 限制分类 | 说明 |
|---|---|
| 桶版本限制 | 仅桶版本号为3.0及以上的桶支持镜像回源。 |
| 时间限制 | 修改镜像回源规则,需要5分钟之后才能生效。 |
| 规则数量限制 | 最多创建10条镜像数据回源规则。 |
| 功能限制 |
|
| 权限限制 |
|
| 其他限制 |
|
方法定义
ObsClient.setBucketMirrorBackToSource(params)
请求参数说明
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| Bucket | string | 必选 | 参数解释: 桶名。 约束限制:
取值范围: 长度为3~63个字符。 默认取值: 无 |
| Rules | string | 必选 | 参数解释: 镜像回源规则列表,JSON格式的字符串。 约束限制:
默认取值: 不涉及 |
返回结果
| 参数名称 | 参数类型 | 描述 |
|---|---|---|
| CommonMsg | 参数解释: 接口调用完成后的公共信息,包含HTTP状态码,操作失败的错误码等,详见ICommonMsg。 | |
| InterfaceResult | 参数解释: 操作成功后的结果数据,详见表6。 约束限制: 当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服务端返回的详细错误码。 |
代码示例
您可以通过ObsClient.setBucketMirrorBackToSource设置镜像回源规则。代码示例如下:
// 引入obs库
// 使用npm安装
const ObsClient = require("esdk-obs-nodejs");
// 使用源码安装
// var ObsClient = require('./lib/obs');
// 创建ObsClient实例
const obsClient = new ObsClient({
// 推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险
// 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
access_key_id: process.env.ACCESS_KEY_ID,
secret_access_key: process.env.SECRET_ACCESS_KEY,
// 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入
// security_token: process.env.SECURITY_TOKEN,
// endpoint填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写
server: "https://obs.cn-north-4.myhuaweicloud.com",
});
async function setBucketMirrorBackToSource() {
try {
// 构建镜像回源规则(JSON字符串)
const rules = JSON.stringify({
rules: [{
id: 'rule-001',
condition: {
httpErrorCodeReturnedEquals: '404',
objectKeyPrefixEquals: 'video/'
},
redirect: {
agency: 'your_agency',
publicSource: {
sourceEndpoint: {
master: ['http://bucket1.example.com'],
slave: ['http://bucket2.example.com']
}
},
passQueryString: true,
mirrorFollowRedirect: false
}
}]
});
const params = {
// 指定存储桶名称
Bucket: "examplebucket",
// 指定镜像回源规则
Rules: rules,
};
// 设置镜像回源规则
const result = await obsClient.setBucketMirrorBackToSource(params);
if (result.CommonMsg.Status <= 300) {
console.log("Set bucket mirror back to source successful!\n");
console.log("RequestId: %s", result.CommonMsg.RequestId);
return;
};
console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
console.log("Status: %d", result.CommonMsg.Status);
console.log("Code: %s", result.CommonMsg.Code);
console.log("Message: %s", result.CommonMsg.Message);
console.log("RequestId: %s", result.CommonMsg.RequestId);
} catch (error) {
console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
console.log(error);
};
};
setBucketMirrorBackToSource();