u
This commit is contained in:
1 parent
07e0001b87
commit
46cbb2f50d
543 files changed
+32576
-4231
No files matched your search
@@ -4,11 +4,12 @@ import (
|
||||
"allapp-go/internal/config"
|
||||
"context"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"io"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
)
|
||||
|
||||
var S3Client *s3.Client
|
||||
@@ -39,7 +40,8 @@ func NewS3Client(cfg *config.Config) {
|
||||
fmt.Println("S3 client and Presign client initialized successfully!")
|
||||
}
|
||||
|
||||
func UploadToRustFS(ctx context.Context, key string, file multipart.File, size int64, contentType string) error {
|
||||
// UploadToRustFS 上传
|
||||
func UploadToRustFS(ctx context.Context, file io.Reader, key string, size int64, contentType string) error {
|
||||
_, err := S3Client.PutObject(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
@@ -47,6 +49,47 @@ func UploadToRustFS(ctx context.Context, key string, file multipart.File, size i
|
||||
ContentLength: aws.Int64(size),
|
||||
ContentType: aws.String(contentType),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteFromRustFS 删除
|
||||
func DeleteFromRustFS(ctx context.Context, key string) error {
|
||||
_, err := S3Client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func DeleteBatch(ctx context.Context, keys []string) error {
|
||||
const maxBatch = 1000
|
||||
|
||||
for i := 0; i < len(keys); i += maxBatch {
|
||||
end := i + maxBatch
|
||||
if end > len(keys) {
|
||||
end = len(keys)
|
||||
}
|
||||
|
||||
var objects []types.ObjectIdentifier
|
||||
for _, k := range keys[i:end] {
|
||||
objects = append(objects, types.ObjectIdentifier{
|
||||
Key: aws.String(k),
|
||||
})
|
||||
}
|
||||
|
||||
_, err := S3Client.DeleteObjects(ctx, &s3.DeleteObjectsInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Delete: &types.Delete{
|
||||
Objects: objects,
|
||||
Quiet: aws.Bool(false), // 返回删除结果(建议保留)
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err // 整批失败才返回
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user