u
This commit is contained in:
1 parent
07e0001b87
commit
46cbb2f50d
543 files changed
+32576
-4231
No files matched your search
@@ -2,6 +2,9 @@ package requestx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3/client"
|
||||
@@ -97,3 +100,31 @@ func PostForm(url string, formData map[string]string, cfg client.Config) (string
|
||||
}
|
||||
return string(resp.Body()), resp.StatusCode(), nil
|
||||
}
|
||||
|
||||
func GetFileStream(ctx context.Context, url string) (io.ReadCloser, int64, string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0")
|
||||
|
||||
httpClient := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
err := resp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
return nil, 0, "", fmt.Errorf("request failed: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return resp.Body, resp.ContentLength, resp.Header.Get("Content-Type"), nil
|
||||
}
|
||||
@@ -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