This commit is contained in:
jiangyong 2026-08-28 12:46:04 +08:00
parent 2ff4235310
commit e126b97031
2 changed files with 36 additions and 30 deletions

8
go.mod
View File

@ -29,7 +29,6 @@ require (
github.com/cloudwego/base64x v0.1.6 // indirect github.com/cloudwego/base64x v0.1.6 // indirect
github.com/fogleman/gg v1.3.0 // indirect github.com/fogleman/gg v1.3.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/gammazero/toposort v0.1.1 // indirect
github.com/gin-contrib/sse v1.1.0 // indirect github.com/gin-contrib/sse v1.1.0 // indirect
github.com/go-ini/ini v1.67.0 // indirect github.com/go-ini/ini v1.67.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
@ -45,21 +44,23 @@ require (
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect
github.com/matishsiao/goInfo v0.0.0-20210923090445-da2e3fa8d45f // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/gomega v1.40.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/quic-go/qpack v0.5.1 // indirect github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.54.0 // indirect github.com/quic-go/quic-go v0.54.0 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/speps/go-hashids v2.0.0+incompatible // indirect github.com/speps/go-hashids v2.0.0+incompatible // indirect
github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.3.0 // indirect github.com/ugorji/go/codec v1.3.0 // indirect
go.uber.org/mock v0.5.0 // indirect go.uber.org/mock v0.5.0 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/arch v0.20.0 // indirect golang.org/x/arch v0.20.0 // indirect
golang.org/x/image v0.43.0 // indirect golang.org/x/image v0.43.0 // indirect
golang.org/x/mod v0.36.0 // indirect golang.org/x/mod v0.36.0 // indirect
@ -69,5 +70,4 @@ require (
golang.org/x/text v0.38.0 // indirect golang.org/x/text v0.38.0 // indirect
golang.org/x/tools v0.45.0 // indirect golang.org/x/tools v0.45.0 // indirect
google.golang.org/protobuf v1.36.9 // indirect google.golang.org/protobuf v1.36.9 // indirect
modernc.org/fileutil v1.0.0 // indirect
) )

View File

@ -22,6 +22,7 @@ type QiniuConfig struct {
type Qiniu struct { type Qiniu struct {
config *QiniuConfig config *QiniuConfig
domains []string
} }
func NewQiniu(cfg *QiniuConfig) Storage { func NewQiniu(cfg *QiniuConfig) Storage {
@ -137,41 +138,25 @@ func (s *Qiniu) List(objectPrefix string) ([]string, error) {
} }
func (s *Qiniu) Get(objectName, fileName string) error { func (s *Qiniu) Get(objectName, fileName string) error {
mac := qbox.NewMac(s.config.AK, s.config.SK) s.loadDomains()
cfg := storage.Config{ if len(s.domains) <= 0 {
UseHTTPS: false,
}
bucketManager := storage.NewBucketManager(mac, &cfg)
domains, err := bucketManager.ListBucketDomains(s.config.Bucket)
if err != nil {
return err
}
if len(domains) <= 0 {
return errors.New("bucket no domain") return errors.New("bucket no domain")
} }
url := fmt.Sprintf("http://%s/%s", domains[0].Domain, objectName) url := fmt.Sprintf("http://%s/%s", s.domains[0], objectName)
return Download(url, fileName) return Download(url, fileName)
} }
func (s *Qiniu) Url(objectName string, expire time.Duration, https ...bool) string { func (s *Qiniu) Url(objectName string, expire time.Duration, https ...bool) string {
mac := qbox.NewMac(s.config.AK, s.config.SK) mac := qbox.NewMac(s.config.AK, s.config.SK)
cfg := storage.Config{
UseHTTPS: false, s.loadDomains()
if len(s.domains) <= 0 {
return ""
} }
bucketManager := storage.NewBucketManager(mac, &cfg) domain := "http://" + s.domains[0]
domains, err := bucketManager.ListBucketDomains(s.config.Bucket)
if err != nil {
return ""
}
if len(domains) <= 0 {
return ""
}
domain := "http://" + domains[0].Domain
if len(https) > 0 && https[0] { if len(https) > 0 && https[0] {
domain = "https://" + domains[0].Domain domain = "https://" + s.domains[0]
} }
if expire == 0 { if expire == 0 {
@ -218,6 +203,27 @@ func isQiniuNotFound(err error) bool {
return false return false
} }
func (s *Qiniu) loadDomains() {
if len(s.domains) != 0 {
return
}
mac := qbox.NewMac(s.config.AK, s.config.SK)
cfg := storage.Config{
UseHTTPS: false,
}
bucketManager := storage.NewBucketManager(mac, &cfg)
domains, err := bucketManager.ListBucketDomains(s.config.Bucket)
if err != nil {
return
}
s.domains = make([]string, 0)
for _, domain := range domains {
s.domains = append(s.domains, domain.Domain)
}
}
func (s *Qiniu) Fetch(url, objectName string, local ...bool) error { func (s *Qiniu) Fetch(url, objectName string, local ...bool) error {
if len(local) > 0 && local[0] == true { if len(local) > 0 && local[0] == true {
return s.fetchLocal(url, objectName) return s.fetchLocal(url, objectName)