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
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue