Skip to content

Commit

Permalink
优化时区 文件上传
Browse files Browse the repository at this point in the history
  • Loading branch information
qida committed Sep 3, 2021
1 parent 16b6e74 commit 083ebcb
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions cdn/qiniu.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ import (
)

type QiNiu struct {
Bucket string
Url string
Mac *qbox.Mac
Config *storage.Config
PutPolicy *storage.PutPolicy
Bucket string
Url string
Mac *qbox.Mac
CdnManager *cdn.CdnManager
Config *storage.Config
PutPolicy *storage.PutPolicy
}

func NewQiNiu(bucket string, url string, accessKey, secretKey string) *QiNiu {
zone, _ := storage.GetZone(accessKey, bucket)
mac := qbox.NewMac(accessKey, secretKey)
cdnManager := cdn.NewCdnManager(mac)
return &QiNiu{
Bucket: bucket,
Url: url,
Mac: qbox.NewMac(accessKey, secretKey),
Bucket: bucket,
Url: url,
Mac: mac,
CdnManager: cdnManager,
Config: &storage.Config{
Zone: zone,
UseHTTPS: false,
Expand Down Expand Up @@ -57,10 +61,18 @@ func (c *QiNiu) Upload(localFile io.Reader, size int64, file_name string) (url_f
}
url_file = ret.Key
fmt.Printf("=====上传======\r\nKey:%s Hash:%s\r\n==============\r\n", ret.Key, ret.Hash)
urlsToRefresh := []string{c.Url + url_file}
cdnManager := cdn.NewCdnManager(c.Mac)
fmt.Printf("=====刷新文件======\r\n%s\r\n==============\r\n", urlsToRefresh)
_, err = cdnManager.RefreshUrls(urlsToRefresh)
err = c.Refresh(c.Url + url_file)
return
}

func (c *QiNiu) Refresh(url ...string) (err error) {
for i := 0; i < len(url); i++ {
if !strings.Contains(url[i], "http:") {
url[i] = "http:" + url[i]
}
}
fmt.Printf("=====刷新文件======\r\n%s\r\n==============\r\n", url)
_, err = c.CdnManager.RefreshUrls(url)
return
}

Expand All @@ -82,10 +94,7 @@ func (c *QiNiu) UploadFile(file_name string, file_data []byte) (url_file string,
}
url_file = ret.Key
fmt.Printf("=====上传======\r\nKey:%s Hash:%s\r\n==============\r\n", ret.Key, ret.Hash)
urlsToRefresh := []string{c.Url + url_file}
cdnManager := cdn.NewCdnManager(c.Mac)
fmt.Printf("=====刷新文件======\r\n%s\r\n==============\r\n", urlsToRefresh)
_, err = cdnManager.RefreshUrls(urlsToRefresh)
err = c.Refresh(c.Url + url_file)
return
}

Expand Down

0 comments on commit 083ebcb

Please sign in to comment.