镜像回源主要用于数据无缝迁移到 TOS 的场景,例如 Web 服务已经在您自建的源站上运行,您将静态网页迁移到 TOS 中存储,可配置镜像回源规则获取未迁移至 TOS 中的部分数据。
注意
import { TosClient, TosClientError, TosServerError } from '@volcengine/tos_harmonyos_sdk'; // 创建客户端 const client = new TosClient({ accessKeyId: "Provide your ak", accessKeySecret: "Provide your sk", securityToken: 'Provide your securityToken', region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,"Provide your region" 填写为 cn-beijing。 endpoint: "Provide your endpoint", // 填写域名地址 }); function handleError(error) { if (error instanceof TosClientError) { console.log('Client Err Msg:', error.message); console.log('Client Err Stack:', error.stack); } else if (error instanceof TosServerError) { console.log('Request ID:', error.requestId); console.log('Response Status Code:', error.statusCode); console.log('Response Header:', error.headers); console.log('Response Err Code:', error.code); console.log('Response Err Msg:', error.message); } else { console.log('unexpected exception, message: ', error); } } const bucketName = 'harmonyos-sdk-test-bucket'; async function main() { async function main() { try { const mirrorRule: MirrorBackRule = { // 规则 ID ID: 'rule1', Condition: { // 触发回源规则的错误码,目前只支持 404 HttpCode: 404, }, Redirect: { // 回源规则跳转类型 RedirectType: RedirectType.RedirectTypeMirror, // 重定向后是否去配置源站拉取数据 FetchSourceOnRedirect: true, // 执行跳转时是否携带请求参数 PassQuery: true, // 镜像回源结果是 3XX 时, 是否跳转到 Location 获取数据 FollowRedirect: true, MirrorHeader: { // 是否透传全部 HTTP HEADER 到源端 PassAll: false, // 透传指定的 HEADER 到源站 Pass: ['header-1', 'header-2'], // 禁止透传指定的 HEADER 到源站 Remove: ['header-3', 'header-4'], }, // 公共可访问的源端地址 PublicSource: { SourceEndpoint: { Primary: ['https://www.example.com'], }, }, }, }; const res = await client.putBucketMirrorBack({ Bucket: bucketName, Rules: [mirrorRule], }); console.log('putBucketMirrorBack Request ID: ', res.RequestId); } catch (error) { handleError(error); } } main();
注意
您必须为桶的所有者,才能获取桶的镜像回源规则。
import { TosClient, TosClientError, TosServerError } from '@volcengine/tos_harmonyos_sdk'; // 创建客户端 const client = new TosClient({ accessKeyId: "Provide your ak", accessKeySecret: "Provide your sk", region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,"Provide your region" 填写为 cn-beijing。 endpoint: "Provide your endpoint", // 填写域名地址 }); function handleError(error) { if (error instanceof TosClientError) { console.log('Client Err Msg:', error.message); console.log('Client Err Stack:', error.stack); } else if (error instanceof TosServerError) { console.log('Request ID:', error.requestId); console.log('Response Status Code:', error.statusCode); console.log('Response Header:', error.headers); console.log('Response Err Code:', error.code); console.log('Response Err Msg:', error.message); } else { console.log('unexpected exception, message: ', error); } } const bucketName = 'harmonyos-sdk-test-bucket'; async function main() { try { const res = await client.getBucketMirrorBack({ Bucket: bucketName, }); console.log('getBucketMirrorBack Request ID: ', res.RequestId); console.log('getBucketMirrorBack Rules Length: ', res.Rules.length); for (const rule of res.Rules) { // 查看规则 ID console.log('rule ID:', rule.ID); // 查看触发回源规则的错误码,目前只支持 404 console.log('rule Condition HttpCode:', rule.Condition.HttpCode); // 查看回源规则跳转类型 console.log('rule Redirect RedirectType:', rule.Redirect.RedirectType); // 重定向后是否去配置源站拉取数据 console.log('rule Redirect FetchSourceOnRedirect:', rule.Redirect.FetchSourceOnRedirect); // 执行跳转时是否携带请求参数 console.log('rule Redirect PassQuery:', rule.Redirect.PassQuery); // 镜像回源结果是 3XX 时, 是否跳转到 Location 获取数据 console.log('rule Redirect FollowRedirect:', rule.Redirect.FollowRedirect); // 是否透传全部 HTTP HEADER 到源端 console.log('rule Redirect MirrorHeader PassAll:', rule.Redirect.MirrorHeader?.PassAll); // 透传指定的 HEADER 到源站 console.log('rule Redirect MirrorHeader Pass:', rule.Redirect.MirrorHeader?.Pass); // 禁止透传指定的 HEADER 到源站 console.log('rule Redirect MirrorHeader Remove:', rule.Redirect.MirrorHeader?.Remove); // 公共可访问的源端地址 console.log('rule Redirect PublicSource SourceEndpoint:', rule.Redirect.PublicSource?.SourceEndpoint); } } catch (error) { handleError(error); } } main();
注意
您必须为桶的所有者,才能删除桶的镜像回源规则。
import { TosClient, TosClientError, TosServerError } from '@volcengine/tos_harmonyos_sdk'; // 创建客户端 const client = new TosClient({ accessKeyId: "Provide your ak", accessKeySecret: "Provide your ak", region: "Provide your region", // 填写 Bucket 所在地域。以华北2(北京)为例,则 "Provide your region" 填写为 cn-beijing。 endpoint: "Provide your endpoint", // 填写域名地址 }); function handleError(error) { if (error instanceof TosClientError) { console.log('Client Err Msg:', error.message); console.log('Client Err Stack:', error.stack); } else if (error instanceof TosServerError) { console.log('Request ID:', error.requestId); console.log('Response Status Code:', error.statusCode); console.log('Response Header:', error.headers); console.log('Response Err Code:', error.code); console.log('Response Err Msg:', error.message); } else { console.log('unexpected exception, message: ', error); } } async function main() { try { const res = await client.deleteBucketMirrorBack({ Bucket: bucketName, }); console.log('deleteBucketMirrorBack Request ID: ', res.RequestId); } catch (error) { handleError(error); } } main();
关于镜像回源的更多信息,请参见设置回源规则。