Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop

This commit is contained in:
laodaming
2023-11-20 17:16:31 +08:00
8 changed files with 106 additions and 0 deletions

View File

@@ -1,12 +1,17 @@
package file
import (
"bufio"
"bytes"
"context"
"fusenapi/model/gmodel"
"fusenapi/utils/basic"
"io"
"net/http"
"os"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
@@ -19,6 +24,16 @@ type Upload struct {
Ctx context.Context
MysqlConn *gorm.DB
AwsSession *session.Session
UploadType string
AliyunOSS AliyunOSS
}
type AliyunOSS struct {
Endpoint string `json:"endpoint"`
AccessKeyId string `json:"access_key_id"`
AccessKeySecret string `json:"access_key_secret"`
BucketName string `json:"bucket_name"`
BucketUrl string `json:"bucket_url"`
BasePath string `json:"base_path"`
}
type UploadBaseReq struct {
@@ -305,3 +320,44 @@ func (upload *Upload) UploadFileByByte(req *UploadBaseReq) (*UploadBaseRes, erro
return &uploadBaseRes, err
}
// 根据URL上传文件
func (upload *Upload) UploadFileByUrl(fileHash string, url string) error {
switch upload.UploadType {
case "oss":
// 创建OSSClient实例。
client, err := oss.New(upload.AliyunOSS.Endpoint, upload.AliyunOSS.AccessKeyId, upload.AliyunOSS.AccessKeySecret)
if err != nil {
return err
}
bucket, err := client.Bucket(upload.AliyunOSS.BucketName)
if err != nil {
return err
}
// 上传文件。
file, err := os.Open(url)
if err != nil {
return err
}
defer file.Close()
yunFileTmpPath := upload.AliyunOSS.BasePath + "/" + fileHash
reader := bufio.NewReader(file)
buffer := make([]byte, 4096)
for {
n, err := reader.Read(buffer)
if err != nil {
if err == io.EOF {
break
}
return err
}
err = bucket.PutObject(yunFileTmpPath, bytes.NewReader(buffer[:n]))
if err != nil {
return err
}
}
return nil
default:
return nil
}
}