RGW静态网站托管功能

背景

静态网站托管(s3website)相比普通s3请求,多了几个核心功能:

  • 默认请求页面。当用户url以"/"结尾时返回的页面。
  • 错误页面。当用户请求的url发生错误时返回的页面。
  • 转发设置。针对一个bucket或者bucket下某个对象设置请求跳转。

除此之外,s3website模式和s3模式相差不大。

设置方法

  • 使用s3cmd设置bucket为静态网站托管模式方法如下:
s3cmd ws-create --ws-index=<default.html> --ws-error=<error.html> s3://<bucket-name>

达到的效果就是当用户访问http://<endpoint>/<bucket-name>/时,会返回s3://<bucket-name>/<default.html>对象,更一般的情况是用户访问http://<endpoint>/<bucket-name>/<path>/时,实际返回s3://<bucket-name>/<path>/<default.html>对象;如果用户访问的url没有对应的s3 object,则返回s3://<bucket-name>/<error.html>对象。

  • 使用REST API设置bucket为静态网站托管的方法如下:
#!/bin/bash
host=<ip>:<normal-rgw-port>
resource="/<bucket_name>"
indexPage="<index.html>"
errorPage="<error.html>"
contentType="XXYYZZ"
dateValue=`date -R -u`
stringToSign="PUT

${contentType}
${dateValue}
${resource}"
s3Key=<key>
s3Secret=<secret>
#计算签名
signature=`/bin/echo -n "$stringToSign" | openssl sha1 -hmac ${s3Secret} -binary | base64`
curl -X PUT \
  -H "Date: ${dateValue}" \
  -H "Content-Type: ${contentType}" \
  -H "Authorization: AWS ${s3Key}:${signature}" \
  -d "<WebsiteConfiguration><IndexDocument><Suffix>${indexPage}</Suffix></IndexDocument><ErrorDocument><Key>${errorPage}</Key></ErrorDocument><RoutingRules><RoutingRule><Condition><KeyPrefixEquals>docs/</KeyPrefixEquals></Condition><Redirect><ReplaceKeyPrefixWith>documents/</ReplaceKeyPrefixWith></Redirect></RoutingRule></RoutingRules></WebsiteConfiguration>" \
  "http://${host}${resource}?website"

注意:相应的GET, DEL HTTP方法可用于获取和清空bucket的静态网站托管配置。

接收请求的RGW需要手动配置的配置项有:

rgw_enable_apis = "s3, s3website, swift, swift_auth, admin"
rgw_enable_static_website = true

使用静态网站托管bucket时需要向特定的RGW发送请求,这些RGW的配置和常规用于s3访问的RGW不同:

rgw_enable_apis="s3website, s3, swift, swift_auth, admin"

代码实现

设置bucket

将bucket设置成静态网站托管模式时,使用的rgw_api仍然是s3,和常规对象存储相同。
url中的website请求参数会标识本次请求是一次“设置bucket为website模式”的请求:

RGWOp *RGWHandler_REST_Bucket_S3::op_put()
{
 ...
  if (s->info.args.sub_resource_exists("website")) {
    if (!s->cct->_conf->rgw_enable_static_website) {
      return NULL;
    }
    return new RGWSetBucketWebsite_ObjStore_S3;
  }
...

使用bucket

为了以website模式对一个bucket进行访问,需要将接收请求的RGW的rgw_api设置成s3website优先。当RGW接收到请求后,会将请求标志成s3website模式

int RGWREST::preprocess(struct req_state *s, RGWClientIO* cio)
{
...
    if (s3website_enabled && api_priority_s3website > api_priority_s3) {
      in_hosted_domain_s3website = 1;
    }

    if (in_hosted_domain_s3website) {
      s->prot_flags |= RGW_REST_WEBSITE;
    }
...
}

在随后的RGWHandler选择中会据此选用相应的Handler

RGWHandler_REST* RGWRESTMgr_S3::get_handler(struct req_state *s)
{
  bool is_s3website = enable_s3website && (s->prot_flags & RGW_REST_WEBSITE);
  int ret = RGWHandler_REST_S3::init_from_header(s,
                    is_s3website ? RGW_FORMAT_HTML :
                    RGW_FORMAT_XML, true);
  if (ret < 0)
    return NULL;

  RGWHandler_REST* handler;
  // TODO: Make this more readable
  if (is_s3website) {
    if (s->init_state.url_bucket.empty()) {
      handler = new RGWHandler_REST_Service_S3Website;
    } else if (s->object.empty()) {
      handler = new RGWHandler_REST_Bucket_S3Website;
    } else {
      handler = new RGWHandler_REST_Obj_S3Website;
    }
...

RGWHandler_REST_Service_S3Website, RGWHandler_REST_Bucket_S3WebsiteRGWHandler_REST_Object_S3Website均继承自RGWHandler_REST_S3Website, 三者在get_obj失败时便会调用RGWHandler_REST_S3Website::error_handler(),从而实现错误界面的返回。
默认页面的返回是由RGWHandler_REST_S3Website::retarget()实现,此函数在rgw_process.cc中被调用:

int process_request(RGWRados* store, RGWREST* rest, RGWRequest* req,
            RGWStreamIO* client_io, OpsLogSocket* olog) 
{
...
/**
   * Only some accesses support website mode, and website mode does NOT apply
   * if you are using the REST endpoint either (ergo, no authenticated access)
   */
  req->log(s, "recalculating target");
  ret = handler->retarget(op, &op);
...

因此retarget动作永远会执行,不论是s3还是s3website。

总结

在RGW中使用静态网站托管功能需要配置两套RGW示例,分别用于配置bucket和访问bucket。且默认页面需要在没个“目录”下放置(这一点可能和其他厂商的对象存储服务有区别),否则会导致404,进而返回错误页面。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • https://aws.amazon.com/cn/s3/faqs/#sia_anchor Amazon Simp...
    守望者_1065阅读 8,380评论 0 5
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,466评论 25 708
  • 看到弟弟一天天长大,除了开心之外,还有一种很浓重的无力感。 弟弟不再是那个小小的肉嘟嘟的两三岁的小小的孩子了,不...
    西瓜半阅读 249评论 0 1
  • 昨天看了电影《滚烫的爱》,全程哭的稀里哗啦,好久都没有影视作品这样让我有强烈的共鸣。女主双叶看年级好像四十岁左右,...
    光年123阅读 958评论 0 0