You've already forked VptPassiveAdapter
feat(api): 支持OSS文件上传时指定Content-Type
- 在UploadFileToOSS函数中新增contentType参数 - 设置请求头Content-Length和Content-Type - 为图片上传指定image/jpeg类型 - 增加上传失败时的日志记录 - 引入zap日志库支持结构化日志输出
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"ZhenTuLocalPassiveAdapter/config"
|
||||
"ZhenTuLocalPassiveAdapter/logger"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
@@ -9,6 +10,8 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// VIID Request/Response Structures
|
||||
@@ -189,11 +192,13 @@ func SubmitFailure(ctx context.Context, taskID int64, errorCode, errorMessage st
|
||||
return nil
|
||||
}
|
||||
|
||||
func UploadFileToOSS(ctx context.Context, uploadUrl string, data []byte) error {
|
||||
func UploadFileToOSS(ctx context.Context, uploadUrl string, data []byte, contentType string) error {
|
||||
req, err := http.NewRequestWithContext(ctx, "PUT", uploadUrl, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Length", fmt.Sprintf("%d", len(data)))
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
|
||||
resp, err := GetUploadClient().Do(req)
|
||||
if err != nil {
|
||||
@@ -202,6 +207,8 @@ func UploadFileToOSS(ctx context.Context, uploadUrl string, data []byte) error {
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
logger.Error("oss upload failed", zap.String("body", string(body)))
|
||||
return fmt.Errorf("oss upload failed: %d", resp.StatusCode)
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user