Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ type UpYunConfig struct {
Secret string // 表单上传密钥,已经弃用!
Hosts map[string]string // 自定义 Hosts 映射关系
UserAgent string // HTTP User-Agent 头,默认 "UPYUN Go SDK V2"
UseHTTP bool // 默认使用https,若要使用http,则该字段值为true
}
```

Expand Down
4 changes: 2 additions & 2 deletions upyun/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (up *UpYun) FormUpload(config *FormUploadConfig) (*FormUploadResp, error) {
formValues["authorization"] = up.MakeUnifiedAuth(sign)
}

endpoint := up.doGetEndpoint("v0.api.upyun.com")
url := fmt.Sprintf("http://%s/%s", endpoint, up.Bucket)
endpoint := up.getEndpoint("v0.api.upyun.com")
url := fmt.Sprintf("%s/%s", endpoint, up.Bucket)
resp, err := up.doFormRequest(url, formValues)
if err != nil {
return nil, err
Expand Down
19 changes: 19 additions & 0 deletions upyun/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package upyun

import (
"bytes"
"fmt"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -80,3 +81,21 @@ func (up *UpYun) doGetEndpoint(host string) string {
}
return host
}

func (up *UpYun) getEndpoint(defaultHost string) string {
value := up.Hosts["host"]
if value == "" {
value = defaultHost
}
s := strings.TrimSpace(value)
s = strings.TrimPrefix(s, "http://")
s = strings.TrimPrefix(s, "https://")
if s == "" {
s = defaultHost
}
scheme := "https://"
if up.UseHTTP {
scheme = "http://"
}
return fmt.Sprintf("%s%s", scheme, s)
}
8 changes: 4 additions & 4 deletions upyun/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func (up *UpYun) doProcessRequest(method, uri string,

var resp *http.Response
var err error
endpoint := up.doGetEndpoint("p0.api.upyun.com")
rawurl := fmt.Sprintf("http://%s%s", endpoint, uri)
endpoint := up.getEndpoint("p0.api.upyun.com")
rawurl := fmt.Sprintf("%s%s", endpoint, uri)
switch method {
case "GET":
resp, err = up.doHTTPRequest(method, rawurl, headers, nil)
Expand Down Expand Up @@ -203,8 +203,8 @@ func (up *UpYun) doSyncProcessRequest(method, uri string, payload string) (map[s

var resp *http.Response
var err error
endpoint := up.doGetEndpoint("p1.api.upyun.com")
rawurl := fmt.Sprintf("http://%s%s", endpoint, uri)
endpoint := up.getEndpoint("p1.api.upyun.com")
rawurl := fmt.Sprintf("%s%s", endpoint, uri)
switch method {
case "POST":
resp, err = up.doHTTPRequest(method, rawurl, headers, strings.NewReader(payload))
Expand Down
4 changes: 2 additions & 2 deletions upyun/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,8 @@ func (up *UpYun) doRESTRequest(config *restReqConfig) (*http.Response, error) {
})
}

endpoint := up.doGetEndpoint("v0.api.upyun.com")
url := fmt.Sprintf("http://%s%s", endpoint, escUri)
endpoint := up.getEndpoint("v0.api.upyun.com")
url := fmt.Sprintf("%s%s", endpoint, escUri)

resp, err := up.doHTTPRequest(config.method, url, headers, config.httpBody)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions upyun/upyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type UpYunConfig struct {
Secret string // deprecated
Hosts map[string]string
UserAgent string
UseHTTP bool
}

type UpYun struct {
Expand Down