fetch
This commit is contained in:
parent
74725d735d
commit
2f86b5dd30
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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
|
||||
|
||||
|
||||
if expire == 0 {
|
||||
return storage.MakePublicURLv2(domain, objectName)
|
||||
} else {
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue