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 (
|
import (
|
||||||
"ZhenTuLocalPassiveAdapter/config"
|
"ZhenTuLocalPassiveAdapter/config"
|
||||||
|
"ZhenTuLocalPassiveAdapter/logger"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -9,6 +10,8 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
// VIID Request/Response Structures
|
// VIID Request/Response Structures
|
||||||
@@ -189,11 +192,13 @@ func SubmitFailure(ctx context.Context, taskID int64, errorCode, errorMessage st
|
|||||||
return nil
|
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))
|
req, err := http.NewRequestWithContext(ctx, "PUT", uploadUrl, bytes.NewReader(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
req.Header.Set("Content-Length", fmt.Sprintf("%d", len(data)))
|
||||||
|
req.Header.Set("Content-Type", contentType)
|
||||||
|
|
||||||
resp, err := GetUploadClient().Do(req)
|
resp, err := GetUploadClient().Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -202,6 +207,8 @@ func UploadFileToOSS(ctx context.Context, uploadUrl string, data []byte) error {
|
|||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
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 fmt.Errorf("oss upload failed: %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"ZhenTuLocalPassiveAdapter/logger"
|
"ZhenTuLocalPassiveAdapter/logger"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
@@ -26,7 +25,7 @@ func UploadFaceData(ctx context.Context, scenicId int64, deviceNo string, faceIm
|
|||||||
// Upload Face Image
|
// Upload Face Image
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
if len(faceImg) > 0 {
|
if len(faceImg) > 0 {
|
||||||
if err := UploadFileToOSS(subCtx, uploadConfig.FaceUploadURL, faceImg); err != nil {
|
if err := UploadFileToOSS(subCtx, uploadConfig.FaceUploadURL, faceImg, "image/jpeg"); err != nil {
|
||||||
return fmt.Errorf("upload face image failed: %w", err)
|
return fmt.Errorf("upload face image failed: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,7 +35,7 @@ func UploadFaceData(ctx context.Context, scenicId int64, deviceNo string, faceIm
|
|||||||
// Upload Thumbnail Image
|
// Upload Thumbnail Image
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
if len(thumbImg) > 0 {
|
if len(thumbImg) > 0 {
|
||||||
if err := UploadFileToOSS(subCtx, uploadConfig.ThumbnailUploadURL, thumbImg); err != nil {
|
if err := UploadFileToOSS(subCtx, uploadConfig.ThumbnailUploadURL, thumbImg, "image/jpeg"); err != nil {
|
||||||
return fmt.Errorf("upload thumbnail image failed: %w", err)
|
return fmt.Errorf("upload thumbnail image failed: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,7 +45,7 @@ func UploadFaceData(ctx context.Context, scenicId int64, deviceNo string, faceIm
|
|||||||
// Upload Source Image
|
// Upload Source Image
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
if len(srcImg) > 0 {
|
if len(srcImg) > 0 {
|
||||||
if err := UploadFileToOSS(subCtx, uploadConfig.SourceUploadURL, srcImg); err != nil {
|
if err := UploadFileToOSS(subCtx, uploadConfig.SourceUploadURL, srcImg, "image/jpeg"); err != nil {
|
||||||
return fmt.Errorf("upload source image failed: %w", err)
|
return fmt.Errorf("upload source image failed: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user