This commit is contained in:
jiangyong 2026-03-17 17:11:50 +08:00
parent 74725d735d
commit 2f86b5dd30
3 changed files with 25 additions and 4 deletions

View File

@ -154,7 +154,7 @@ func (s *Minio) Stat(objectName string) (*ObjectInfo, error) {
return info, nil return info, nil
} }
func (s *Minio) Fetch(urlStr, objectName string) error { func (s *Minio) Fetch(urlStr, objectName string, local ...bool) error {
if err := s.Init(); err != nil { if err := s.Init(); err != nil {
return err return err
} }

View File

@ -5,6 +5,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"path"
"strings"
"time" "time"
"github.com/qiniu/go-sdk/v7/auth/qbox" "github.com/qiniu/go-sdk/v7/auth/qbox"
@ -169,7 +171,7 @@ func (s *Qiniu) Url(objectName string, expire time.Duration) string {
} }
domain := "https://" + domains[0].Domain domain := "https://" + domains[0].Domain
if expire == 0 { if expire == 0 {
return storage.MakePublicURLv2(domain, objectName) return storage.MakePublicURLv2(domain, objectName)
} else { } else {
@ -198,7 +200,10 @@ func (s *Qiniu) Stat(objectName string) (*ObjectInfo, error) {
return info, nil return info, nil
} }
func (s *Qiniu) Fetch(url, objectName string) error { func (s *Qiniu) Fetch(url, objectName string, local ...bool) error {
if len(local) > 0 && local[0] == true {
return s.fetchLocal(url, objectName)
}
mac := qbox.NewMac(s.config.AK, s.config.SK) mac := qbox.NewMac(s.config.AK, s.config.SK)
cfg := storage.Config{ cfg := storage.Config{
UseHTTPS: false, UseHTTPS: false,
@ -213,3 +218,19 @@ func (s *Qiniu) Fetch(url, objectName string) error {
return nil return nil
} }
func (s *Qiniu) fetchLocal(url, objectName string) error {
objectName = strings.TrimLeft(objectName, "/ ")
ext := strings.TrimLeft(path.Ext(objectName), ".")
tmpFile := fmt.Sprintf("%d.%s", time.Now().UnixMilli(), ext)
if err := Download(url, tmpFile); err != nil {
return err
}
defer os.Remove(tmpFile)
if err := s.Put(tmpFile, objectName, nil); err != nil {
return err
}
return nil
}

View File

@ -16,5 +16,5 @@ type Storage interface {
Url(objectName string, expire time.Duration) string Url(objectName string, expire time.Duration) string
Stat(objectName string) (*ObjectInfo, error) Stat(objectName string) (*ObjectInfo, error)
List(objectPrefix string) ([]string, error) List(objectPrefix string) ([]string, error)
Fetch(url, objectName string) error Fetch(url, objectName string, local ...bool) error
} }