您可以将存储空间配置成静态网站托管模式。配置生效后,访问网站相当于访问存储空间,并且能够自动跳转至指定的索引页面和错误页面。

自定义域名绑定中 提到,OSS允许用户将自己的域名指向OSS服务的地址。这样用户访问他的网站的 时候,实际上是在访问OSS的Bucket。对于网站,需要指定首页(index)和出错页 (error)分别对应的Bucket中的文件名。

更多关于静态网站托管的介绍,请参见开发指南中的配置静态网站托管

设置托管页面

通过putBucketWebsite来设置托管页面:

let OSS = require('ali-oss')

let client = new OSS({
  region: '<Your region>'
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
  bucket: '<Your bucket name>'
});

async function putBucketWebsite () {
  try {
    let result = await client.putBucketWebsite('bucket-name', {
    index: 'index.html',
    error: 'error.html'
  });
   console.log(result);
  } catch (e) {
    console.log(e);
  }
}

putBucketWebsite();

查看托管页面

通过getBucketWebsite来查看托管页面:

let OSS = require('ali-oss')

let client = new OSS({
  region: '<Your region>'
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
  bucket: '<Your bucket name>'
});

async function getBucketWebsite () {
  try {
    let result = await client.getBucketWebsite('bucket-name');
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

getBucketWebsite();

清除托管页面

通过deleteBucketWebsite来清除托管页面:

let OSS = require('ali-oss')

let client = new OSS({
  region: '<Your region>'
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
  bucket: '<Your bucket name>'
});

async function deleteBucketWebsite() {
  try {
    let result = await client.deleteBucketWebsite('bucket-name');
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

deleteBucketWebsite();