Skip to content

Commit

Permalink
fix: handler GetBucketWebsite error
Browse files Browse the repository at this point in the history
set staticWebsiteConfig firstly will get 404 error, pass it.
  • Loading branch information
fangbinwei committed Sep 14, 2020
1 parent 4c653ca commit 611f87b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ENDPOINT = b.fangbinwei.cn
ENDPOINT = fangbinwei-blog.oss-cn-shanghai.aliyuncs.com
FOLDER = example
BUCKET = fangbinwei-blog
INDEX_PAGE = index.html
Expand Down
6 changes: 6 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (

func TestMain(t *testing.T) {
assert := assert.New(t)

err := operation.SetStaticWebsiteConfig(config.Client, config.Website)
assert.NoError(err)

errs := operation.DeleteObjects(config.Bucket)
assert.Equal(len(errs), 0)

Expand Down Expand Up @@ -41,6 +45,8 @@ func TestMain(t *testing.T) {
prefix += "/"
}
for _, u := range uploaded {
// 如果自定义域名解析到了cdn, 这个接口会报错, 但是上面的测试流程正常
// 避开方法: env中endpoint使用bucket的endpoint或者bucket域名, 而不是自定义域名
props, err := config.Bucket.GetObjectDetailedMeta(strings.TrimPrefix(u.PathOSS, prefix))
assert.NoError(err)
cacheControl := props.Get("Cache-Control")
Expand Down
12 changes: 9 additions & 3 deletions operation/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import (
)

// SetStaticWebsiteConfig is used to set some option of website, like redirect strategy, index page, 404 page.
func SetStaticWebsiteConfig(client *oss.Client, o *config.WebsiteOptions) {
func SetStaticWebsiteConfig(client *oss.Client, o *config.WebsiteOptions) error {
bEnable := true
supportSubDirType := 0
websiteDetailConfig, err := client.GetBucketWebsite(config.Bucket.BucketName)
if err != nil {
fmt.Println("Failed to get website detail configuration, skip setting")
return
serviceError, ok := err.(oss.ServiceError)
// 404 means NoSuchWebsiteConfiguration
if !ok || serviceError.StatusCode != 404 {
fmt.Println("Failed to get website detail configuration, skip setting", err)
return err
}
}
wxml := oss.WebsiteXML(websiteDetailConfig)
wxml.IndexDocument.Suffix = o.IndexPage
Expand All @@ -26,5 +30,7 @@ func SetStaticWebsiteConfig(client *oss.Client, o *config.WebsiteOptions) {
err = client.SetBucketWebsiteDetail(config.BucketName, wxml)
if err != nil {
fmt.Printf("Failed to set website detail configuration: %v\n", err)
return err
}
return nil
}

0 comments on commit 611f87b

Please sign in to comment.