feat: add s3 storage provider integration

This commit is contained in:
2026-01-13 16:46:00 +08:00
parent bd8dab5764
commit e97be37b05
9 changed files with 253 additions and 22 deletions

View File

@@ -349,20 +349,20 @@ func (s *common) CompleteUpload(ctx context.Context, tenantID, userID int64, for
Meta: existing.Meta,
}
} else {
finalPath := filepath.Join(localPath, objectKey)
if err := os.MkdirAll(filepath.Dir(finalPath), 0o755); err != nil {
return nil, errorx.ErrInternalError.WithCause(err)
contentType := strings.TrimSpace(meta.MimeType)
if contentType == "" {
contentType = "application/octet-stream"
}
if err := os.Rename(mergedPath, finalPath); err != nil {
return nil, errorx.ErrInternalError.WithCause(err)
if err := s.storage.PutObject(ctx, objectKey, mergedPath, contentType); err != nil {
return nil, errorx.ErrInternalError.WithCause(err).WithMsg("上传存储失败")
}
asset = &models.MediaAsset{
TenantID: tid,
UserID: userID,
Type: consts.MediaAssetType(meta.Type),
Status: consts.MediaAssetStatusUploaded,
Provider: "local",
Bucket: "default",
Provider: s.storage.Provider(),
Bucket: s.storage.Bucket(),
ObjectKey: objectKey,
Hash: hash,
Meta: types.NewJSONType(fields.MediaAssetMeta{
@@ -513,12 +513,12 @@ func (s *common) Upload(
Meta: existing.Meta,
}
} else {
dstPath := filepath.Join(localPath, objectKey)
if err := os.MkdirAll(filepath.Dir(dstPath), 0o755); err != nil {
return nil, errorx.ErrInternalError.WithCause(err).WithMsg("failed to create storage directory")
contentType := strings.TrimSpace(file.Header.Get("Content-Type"))
if contentType == "" {
contentType = "application/octet-stream"
}
if err := os.Rename(tmpPath, dstPath); err != nil {
return nil, errorx.ErrInternalError.WithCause(err).WithMsg("failed to finalize file")
if err := s.storage.PutObject(ctx, objectKey, tmpPath, contentType); err != nil {
return nil, errorx.ErrInternalError.WithCause(err).WithMsg("上传存储失败")
}
os.RemoveAll(tmpDir)
@@ -528,8 +528,8 @@ func (s *common) Upload(
UserID: userID,
Type: consts.MediaAssetType(typeArg),
Status: consts.MediaAssetStatusUploaded,
Provider: "local",
Bucket: "default",
Provider: s.storage.Provider(),
Bucket: s.storage.Bucket(),
ObjectKey: objectKey,
Hash: hash,
Meta: types.NewJSONType(fields.MediaAssetMeta{