miniofetchurl

This commit is contained in:
jiangyong 2026-03-13 10:33:23 +08:00
parent 086162dc67
commit 3458c6fc4e
2 changed files with 48 additions and 8 deletions

18
go.mod
View File

@ -7,7 +7,9 @@ require (
github.com/bradfitz/gomemcache v0.0.0-20250403215159-8d39553ac7cf
github.com/eclipse/paho.mqtt.golang v1.5.1
github.com/gin-gonic/gin v1.11.0
github.com/go-redis/redis v6.15.9+incompatible
github.com/gomodule/redigo v1.9.2
github.com/google/uuid v1.6.0
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
github.com/minio/minio-go v6.0.14+incompatible
github.com/qiniu/go-sdk/v7 v7.25.4
@ -15,7 +17,7 @@ require (
github.com/spf13/cast v1.10.0
github.com/tidwall/gjson v1.18.0
github.com/wechatpay-apiv3/wechatpay-go v0.2.21
golang.org/x/crypto v0.42.0
golang.org/x/crypto v0.47.0
gorm.io/gorm v1.31.0
)
@ -47,6 +49,8 @@ require (
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.39.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.54.0 // indirect
@ -58,12 +62,12 @@ require (
go.uber.org/mock v0.5.0 // indirect
golang.org/x/arch v0.20.0 // indirect
golang.org/x/image v0.31.0 // indirect
golang.org/x/mod v0.27.0 // indirect
golang.org/x/net v0.44.0 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/text v0.29.0 // indirect
golang.org/x/tools v0.36.0 // indirect
golang.org/x/mod v0.32.0 // indirect
golang.org/x/net v0.49.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.40.0 // indirect
golang.org/x/text v0.33.0 // indirect
golang.org/x/tools v0.41.0 // indirect
google.golang.org/protobuf v1.36.9 // indirect
modernc.org/fileutil v1.0.0 // indirect
)

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/minio/minio-go"
"net/http"
"net/url"
"path"
"strings"
@ -131,6 +132,41 @@ func (s *Minio) Stat(objectName string) (*ObjectInfo, error) {
return nil, nil
}
func (s *Minio) Fetch(url, objectName string) error {
func (s *Minio) Fetch(urlStr, objectName string) error {
if err := s.Init(); err != nil {
return err
}
// 从 URL 下载文件
resp, err := http.Get(urlStr)
if err != nil {
log.Errorf("fetch file from url error:%s", err.Error())
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("fetch file from url failed, status: %d", resp.StatusCode)
}
// 获取文件名和 Content-Type
objectName = strings.TrimLeft(objectName, "/ ")
contentType := resp.Header.Get("Content-Type")
if contentType == "" {
ext := strings.TrimLeft(path.Ext(objectName), ".")
contentType = ext2ContentType(ext)
}
// 上传到 MinIO
ctx, cancel := context.WithTimeout(context.Background(), s.config.Timeout)
defer cancel()
_, err = s.client.PutObjectWithContext(ctx, s.config.Bucket, objectName, resp.Body, resp.ContentLength,
minio.PutObjectOptions{ContentType: contentType})
if err != nil {
log.Errorf("upload fetched file to minio error:%s", err.Error())
return err
}
return nil
}