u
This commit is contained in:
1 parent
b6415c0d65
commit
89aaef1d6b
29 files changed
+1118
-277
No files matched your search
@@ -0,0 +1,79 @@
|
||||
package handle
|
||||
|
||||
import (
|
||||
"allapp-go/internal/errors"
|
||||
"allapp-go/internal/httpx"
|
||||
"allapp-go/pkg/s3store"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func S3ListBuckets(c fiber.Ctx) error {
|
||||
result, err := s3store.S3Client.ListBuckets(c.Context(), &s3.ListBucketsInput{})
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
buckets := make([]string, 0, len(result.Buckets))
|
||||
for _, b := range result.Buckets {
|
||||
if b.Name != nil {
|
||||
buckets = append(buckets, *b.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return httpx.OK(c, buckets)
|
||||
}
|
||||
|
||||
func S3UploadFile(c fiber.Ctx) error {
|
||||
fileHeader, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
file, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
defer func(file multipart.File) {
|
||||
_ = file.Close()
|
||||
}(file)
|
||||
|
||||
// 日期目录
|
||||
now := time.Now()
|
||||
folderPath := fmt.Sprintf("%d/%02d/%02d",
|
||||
now.Year(),
|
||||
now.Month(),
|
||||
now.Day(),
|
||||
)
|
||||
|
||||
// UUID 文件名(保留后缀)
|
||||
ext := filepath.Ext(fileHeader.Filename)
|
||||
newName := fmt.Sprintf("%s%s", uuid.NewString(), ext)
|
||||
|
||||
// S3 object key
|
||||
key := fmt.Sprintf("%s/%s", folderPath, newName)
|
||||
|
||||
// ContentType(防止前端乱传)
|
||||
contentType := fileHeader.Header.Get("Content-Type")
|
||||
|
||||
// 调用存储层
|
||||
err = s3store.UploadToRustFS(
|
||||
c.Context(),
|
||||
key,
|
||||
file,
|
||||
fileHeader.Size,
|
||||
contentType,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return httpx.OK(c, nil)
|
||||
}
|
||||
Reference in new issue
Block a user