This commit is contained in:
2025-08-02 02:54:21 +08:00
parent 934dce2776
commit 66845d3fe0
74 changed files with 8686 additions and 212 deletions

View File

@@ -7,6 +7,7 @@ import (
"encoding/base64"
"fmt"
"io"
"net/http"
"path/filepath"
"strings"
"time"
@@ -279,3 +280,56 @@ func (s *QiNiuStorageService) UploadFromReader(ctx context.Context, reader io.Re
return s.UploadFile(ctx, fileBytes, fileName)
}
// DownloadFile 从七牛云下载文件
func (s *QiNiuStorageService) DownloadFile(ctx context.Context, fileURL string) ([]byte, error) {
s.logger.Info("开始从七牛云下载文件", zap.String("file_url", fileURL))
// 创建HTTP客户端
client := &http.Client{
Timeout: 30 * time.Second,
}
// 创建请求
req, err := http.NewRequestWithContext(ctx, "GET", fileURL, nil)
if err != nil {
return nil, fmt.Errorf("创建请求失败: %w", err)
}
// 发送请求
resp, err := client.Do(req)
if err != nil {
s.logger.Error("下载文件失败",
zap.String("file_url", fileURL),
zap.Error(err),
)
return nil, fmt.Errorf("下载文件失败: %w", err)
}
defer resp.Body.Close()
// 检查响应状态
if resp.StatusCode != http.StatusOK {
s.logger.Error("下载文件失败,状态码异常",
zap.String("file_url", fileURL),
zap.Int("status_code", resp.StatusCode),
)
return nil, fmt.Errorf("下载文件失败,状态码: %d", resp.StatusCode)
}
// 读取文件内容
fileContent, err := io.ReadAll(resp.Body)
if err != nil {
s.logger.Error("读取文件内容失败",
zap.String("file_url", fileURL),
zap.Error(err),
)
return nil, fmt.Errorf("读取文件内容失败: %w", err)
}
s.logger.Info("文件下载成功",
zap.String("file_url", fileURL),
zap.Int("file_size", len(fileContent)),
)
return fileContent, nil
}