OpenStack Object Storage Developer Guide/Swift官方API文档 -- 翻译 ,五

OK啦,这是最后一篇,其实从原官方文档的组织上,该小节是3.3——container之后,object之前,为了API整体思路的连贯性,我自己擅作主张放到了最后一部分。这章是我觉得swift中比较有趣的一章,可以直接将swift配置成静态站点,作为数据下载站,直接解决了静态站点数据备份的问题。

于是,让我们愉快的继续吧 =D

3.3. 创建静态站点(Create Static Website)

你可以使用swift账户在互联网上创建一个静态站点。这种模式通常是匿名请求的,如果你希望在这种模式下使用认证请求,则需要在请求头中将 X-Web-Mode 设置为 TRUE。过滤器 staticweb 也应该添加到配置文件 /etc/swift/proxy-server.conf 中认证中间件后的管道上。staticweb 中间件的配置必需添加到pipeline后,如:

[DEFAULT]
...

[pipeline:main]
pipeline = healthcheck cache tempauth staticweb proxy-server

...

[filter:staticweb]
use = egg:swift#staticweb
# Seconds to cache container x-container-meta-web-* header values.
# cache_timeout = 300
# You can override the default log routing for this filter here:
# set log_name = staticweb
# set log_facility = LOG_LOCAL0
# set log_level = INFO
# set access_log_name = staticweb
# set access_log_facility = LOG_LOCAL0
# set access_log_level = INFO
# set log_headers = False

你的公共可读的container应该包含 X-Container-Meta-Web-IndexX-Container-Meta-Web-Error 这两个头( X-Container-Meta-Web-Error 这个header将在后文“为静态站点设置出错页”中讨论)。

X-Container-Meta-Web-Index 用于指定站点的默认访问页(default页,或者index.html文件)。当一个用户第一次进入你的站点时,他不需要指定特殊的index文件,你的站点就会自动将index.html展示给他。如果你通过创建伪目录的方式为你的站点创建了子目录,则每个子目录的默认访问页的名称都将是X-Container-Meta-Web-Index 指定的页面。如果你的伪目录中不包含一个同X-Container-Meta-Web-Index 指定的页面名字相同的index file,则当用户访问这个子目录时,将会返回一个404错误。

你也可以选择展示子目录中的文件列表,而非一个网页。如果要实现展示子目录中的文件列表功能,只需将头 X-Container-Meta-Web-Listtings 设置为 TRUE。你可以通过设置 X-Container-Meta-Web-Listings-CSS 为你的文件列表添加样式(如lists.css)。

3.3.1. 通过Swift实现的静态网页中间件(Static Web Middleware via swift)

  例3.38. 设置Container为公共可读的

  设置Container为公共可读的。一旦container被设置成公共可读的,那么你可以直接访问的容器中的对象,但是你需要为站点的主URL(即container的URL)和它的子目录设置index file。

swift post -r '.r:*' container

  例3.39. 设置站点的Index文件

  设置站点的Index文件。在这个例子中,index.html是站点的默认访问页。

swift post -m 'web-index:index.html' container

  例3.40. 启用文件列表

  启用文件列表。如果你没有设置index文件,则要将container中的对象列表展示出来。设置样式的命令在下一个例子中。

swift post -m 'web-listings: true' container

  例3.41. 为文件列表设置CSS样式

  为文件列表设置样式。

swift post -m 'web-listings-css:listings.css' container

3.3.2. 为静态站点设置出错页(Set Error Pages for Static Website)

你可能需要为你的站点设置自定义的错误页。目前,只支持401(Unauthorized)和404(Not Found)这两个错误。为了实现自定义错误页,你需要设置元数据头 X-Container-Meta-Web-Error

真正的错误页根据 状态码 和你所设置的 X-Container-Meta-Web-Error名称 来决定。例如,你将 X-Container-Meta-Web-Error 设置为 error.html,则当出现401错误时,将会展示 401error.html 页面,类似的,当出现404错误时,将会展示 404error.html 页面。在你设置为container设置 X-Container-Meta-Web-Error 后,你可以为这两种错误分别创建错误页,也可以不创建,让他们访问公共的错误页。

你只需要为整个站点设置一次 X-Container-Meta-Web-Error 即可。

  例3.42. 为文静态站点设置错误页

swift post -m 'web-error:error.html' container

任何一个2xx的响应都表示执行成功了。

至此,OpenStack对象存储的官方API算是翻译完成啦!不要问我第1章和第4章,我是死活都不会翻的!第4章主要是使用cURL与swift交互的介绍,比较直接,没太多需要理解的东西,自己看看就好,不要懒哦 =P