VptPassiveAdapter/api/oss_upload.go
2025-04-13 11:33:55 +08:00

76 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"ZhenTuLocalPassiveAdapter/dto"
"bytes"
"context"
"fmt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"io"
"log"
"net/http"
"os"
)
func UploadTaskFile(ctx context.Context, task dto.Task, file dto.FileObject) error {
url, err := QueryUploadUrlForTask(ctx, task.TaskID)
if err != nil {
return err
}
log.Printf("开始上传文件, URL【%s】\n", url)
if err := OssUpload(ctx, url, file.URL); err != nil {
return err
}
return nil
}
func OssUpload(ctx context.Context, url, filePath string) error {
_, span := tracer.Start(ctx, "OssUpload")
defer span.End()
span.SetAttributes(attribute.String("file.path", filePath))
// 使用 http put 请求上传文件
file, err := os.Open(filePath)
defer os.Remove(filePath)
defer file.Close()
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "文件打开失败")
return err
}
fileBytes, err := io.ReadAll(file)
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "文件读取失败")
return err
}
span.SetAttributes(attribute.Int("file.size", len(fileBytes)))
span.SetAttributes(attribute.String("http.url", url))
span.SetAttributes(attribute.String("http.method", "PUT"))
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(fileBytes))
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "创建请求失败")
return err
}
req.Header.Set("Content-Type", "video/mp4")
req.Header.Set("Content-Length", fmt.Sprintf("%d", len(fileBytes)))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "发送请求失败")
return err
}
defer resp.Body.Close()
span.SetAttributes(attribute.String("http.status", resp.Status))
span.SetAttributes(attribute.Int("http.status_code", resp.StatusCode))
if resp.StatusCode != http.StatusOK {
span.SetStatus(codes.Error, "上传失败")
return fmt.Errorf("upload failed with status code %d", resp.StatusCode)
}
span.SetStatus(codes.Ok, "上传成功")
return nil
}