TOS 支持托管静态网站,您可以将静态网站的所有内容存储至 TOS 存储桶,配置静态网站规则后,即可通过存储桶域名访问该网站。
注意
Content-Disposition:attachment
,即从浏览器访问网页类型文件时,将不会直接预览网站,而会将网站的内容下载到本地。以下代码用于设置重定向所有的请求到另外一个站点。
// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk'; // 创建客户端 const client = new TosClient({ accessKeyId: process.env['TOS_ACCESS_KEY'], accessKeySecret: process.env['TOS_SECRET_KEY'], 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 = 'node-sdk-test-bucket'; async function main() { try { // 场景1 重定向所有的请求到另一个站点 const { requestId: putWebsiteRequestId } = await client.putBucketWebsite({ bucket: bucketName, redirectAllRequestsTo: { HostName: 'www.example.com', Protocol: 'http', }, }); console.log('putBucketReplication Request ID: ', putWebsiteRequestId); // 场景2 根据规则重定向请求 const { requestId: putWebsiteRequestId2 } = await client.putBucketWebsite({ bucket: bucketName, indexDocument: { Suffix: 'index.html', }, errorDocument: { Key: 'error.html', }, // 前缀匹配,获取重定向结果 routingRules: [ { // 重定向规则的条件配置 Condition: { // 指定重定向规则的对象键前缀匹配条件 KeyPrefixEquals: 'prefix', // 指定重定向规则的错误码匹配条件 HttpErrorCodeReturnedEquals: 403, }, // 重定向规则的具体重定向目标配置 Redirect: { // 指定重定向规则的目标协议 Protocol: 'https', HostName: 'www.redirect.com', }, }, ], }); console.log('putBucketReplication Request ID: ', putWebsiteRequestId2); } catch (error) { handleError(error); } } main();
注意
要获取桶的网站配置规则,默认您必须为桶所有者。
以下代码用于获取桶的网站配置规则。
// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk'; // 创建客户端 const client = new TosClient({ accessKeyId: process.env['TOS_ACCESS_KEY'], accessKeySecret: process.env['TOS_SECRET_KEY'], 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 = 'node-sdk-test-bucket'; async function main() { try { const { data, requestId: getWebsiteRequestId } = await client.getBucketWebsite({ bucket: bucketName, }); console.log('getBucketWebsite Request ID: ', getWebsiteRequestId); if (data.RedirectAllRequestsTo != null) { console.log('getBucketWebsite RedirectAllRequestsTo HostName:', data.RedirectAllRequestsTo.HostName); console.log('getBucketWebsite RedirectAllRequestsTo Protocol:', data.RedirectAllRequestsTo.Protocol); } if (data.IndexDocument != null) { console.log('getBucketWebsite IndexDocument ForbiddenSubDir:', data.IndexDocument.ForbiddenSubDir); console.log('getBucketWebsite IndexDocument Suffix:', data.IndexDocument.Suffix); } if (data.ErrorDocument != null) { console.log('getBucketWebsite ErrorDocument Key:', data.ErrorDocument.Key); } for (const rule of data.RoutingRules || []) { console.log('getBucketWebsite RoutingRules Rule Condition HttpErrorCodeReturnedEquals:', rule.Condition.HttpErrorCodeReturnedEquals); console.log('getBucketWebsite RoutingRules Rule Condition KeyPrefixEquals:', rule.Condition.KeyPrefixEquals); console.log('getBucketWebsite RoutingRules Rule Redirect HostName:', rule.Redirect.HostName); console.log('getBucketWebsite RoutingRules Rule Redirect Protocol:', rule.Redirect.Protocol); console.log('getBucketWebsite RoutingRules Rule Redirect ReplaceKeyWith:', rule.Redirect.ReplaceKeyWith); console.log('getBucketWebsite RoutingRules Rule Redirect ReplaceKeyPrefixWith:', rule.Redirect.ReplaceKeyPrefixWith); console.log('getBucketWebsite RoutingRules Rule Redirect HttpRedirectCode:', rule.Redirect.HttpRedirectCode); } } catch (error) { handleError(error); } } main();
注意
要删除桶的网站配置规则,默认您必须为桶所有者。
以下代码用于删除桶的网站配置规则。
// 导入 SDK, 当 TOS Node.JS SDK 版本小于 2.5.2 请把下方 TosClient 改成 TOS 导入 import { TosClient, TosClientError, TosServerError } from '@volcengine/tos-sdk'; // 创建客户端 const client = new TosClient({ accessKeyId: process.env['TOS_ACCESS_KEY'], accessKeySecret: process.env['TOS_SECRET_KEY'], 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 = 'node-sdk-test-bucket'; async function main() { try { const { requestId: deleteWebsiteRquestId } = await client.deleteBucketWebsite({ bucket: bucketName, }); console.log('deleteBucketWebsite Request ID: ', deleteWebsiteRquestId); } catch (error) { handleError(error); } } main();
关于设置静态网站配置的更多信息,请参见设置静态网站。