This commit is contained in:
2025-11-02 20:33:28 +08:00
parent bb88c78c82
commit 2773c1a60b
13 changed files with 521 additions and 132 deletions

View File

@@ -285,9 +285,9 @@ func (s *QiNiuStorageService) UploadFromReader(ctx context.Context, reader io.Re
func (s *QiNiuStorageService) DownloadFile(ctx context.Context, fileURL string) ([]byte, error) {
s.logger.Info("开始从七牛云下载文件", zap.String("file_url", fileURL))
// 创建HTTP客户端
// 创建HTTP客户端超时时间设置为60秒
client := &http.Client{
Timeout: 30 * time.Second,
Timeout: 60 * time.Second,
}
// 创建请求
@@ -299,11 +299,29 @@ func (s *QiNiuStorageService) DownloadFile(ctx context.Context, fileURL string)
// 发送请求
resp, err := client.Do(req)
if err != nil {
s.logger.Error("下载文件失败",
// 检查是否是超时错误
isTimeout := false
if ctx.Err() == context.DeadlineExceeded {
isTimeout = true
} else if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() {
isTimeout = true
} else if errStr := err.Error();
errStr == "context deadline exceeded" ||
errStr == "timeout" ||
errStr == "Client.Timeout exceeded" ||
errStr == "net/http: request canceled" {
isTimeout = true
}
errorMsg := "下载文件失败"
if isTimeout {
errorMsg = "下载文件超时"
}
s.logger.Error(errorMsg,
zap.String("file_url", fileURL),
zap.Error(err),
)
return nil, fmt.Errorf("下载文件失败: %w", err)
return nil, fmt.Errorf("%s: %w", errorMsg, err)
}
defer resp.Body.Close()