静态网站托管
更新时间:2026-09-23
静态网站托管相关方法
通过 Python SDK 可以为 BOS Bucket 设置静态网站托管,指定首页文件和 404 页面文件。
运行以下示例前,请先参考初始化完成 BOS Client 初始化,并将示例中的 bucket_name(及其它占位符)替换为实际值。
| 参数 | 必填 | 说明 |
|---|---|---|
bucket_name |
是 | 已创建的 BOS Bucket 名称。 |
index |
否 | 静态网站首页文件名称,例如 index.html。 |
not_found |
否 | 访问不存在资源时返回的 404 页面文件名称,例如 404.html。 |
代码示例
Python
1bucket_name = "example-bucket-name"
2
3## 设置静态网站托管
4put_response = bos_client.put_bucket_static_website(
5 bucket_name,
6 index="index.html",
7 not_found="404.html",
8)
9
10## 查看静态网站托管
11get_response = bos_client.get_bucket_static_website(bucket_name)
12print(get_response.index)
13print(get_response.not_found)
14
15## 删除静态网站托管
16delete_response = bos_client.delete_bucket_static_website(bucket_name)
代码说明:
| 方法 | 返回值说明 |
|---|---|
put_bucket_static_website |
设置成功时返回 BceResponse,响应体为空,可通过 put_response.metadata 查看响应元数据。 |
get_bucket_static_website |
查询成功时返回 BceResponse,包含 index、not_found 和 not_found_http_status(返回 404 页面时的 HTTP 状态码)等字段。Python SDK 会将响应体中的 notFound、notFoundHttpStatus 分别转换为 not_found、not_found_http_status。 |
delete_bucket_static_website |
删除成功时返回 BceResponse,响应体为空,可通过 delete_response.metadata 查看响应元数据。 |
示例输出:
Text
1index.html
2404.html
评价此篇文章
