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
}
func (s *Minio) Fetch(urlStr, objectName string) error {
func (s *Minio) Fetch(urlStr, objectName string, local ...bool) error {
if err := s.Init(); err != nil {
return err
}

View File

@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"os"
"path"
"strings"
"time"
"github.com/qiniu/go-sdk/v7/auth/qbox"
@ -198,7 +200,10 @@ func (s *Qiniu) Stat(objectName string) (*ObjectInfo, error) {
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)
cfg := storage.Config{
UseHTTPS: false,
@ -213,3 +218,19 @@ func (s *Qiniu) Fetch(url, objectName string) error {
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
Stat(objectName string) (*ObjectInfo, error)
List(objectPrefix string) ([]string, error)
Fetch(url, objectName string) error
Fetch(url, objectName string, local ...bool) error
}