MCPcopy
hub / github.com/flipped-aurora/gin-vue-admin / UploadFile

Method UploadFile

server/utils/upload/cloudflare_r2.go:21–45  ·  view source on GitHub ↗
(file *multipart.FileHeader)

Source from the content-addressed store, hash-verified

19type CloudflareR2 struct{}
20
21func (c *CloudflareR2) UploadFile(file *multipart.FileHeader) (fileUrl string, fileName string, err error) {
22 client := c.newR2Client()
23 uploader := manager.NewUploader(client)
24
25 fileKey := fmt.Sprintf("%d_%s", time.Now().Unix(), file.Filename)
26 fileName = fmt.Sprintf("%s/%s", global.GVA_CONFIG.CloudflareR2.Path, fileKey)
27 f, openError := file.Open()
28 if openError != nil {
29 global.GVA_LOG.Error("function file.Open() failed", zap.Any("err", openError.Error()))
30 return "", "", errors.New("function file.Open() failed, err:" + openError.Error())
31 }
32 defer f.Close() // 创建文件 defer 关闭
33
34 _, err = uploader.Upload(context.TODO(), &s3.PutObjectInput{
35 Bucket: aws.String(global.GVA_CONFIG.CloudflareR2.Bucket),
36 Key: aws.String(fileName),
37 Body: f,
38 })
39 if err != nil {
40 global.GVA_LOG.Error("function uploader.Upload() failed", zap.Any("err", err.Error()))
41 return "", "", err
42 }
43
44 return fmt.Sprintf("%s/%s", global.GVA_CONFIG.CloudflareR2.BaseURL, fileName), fileKey, nil
45}
46
47func (c *CloudflareR2) DeleteFile(key string) error {
48 client := c.newR2Client()

Callers

nothing calls this directly

Calls 6

newR2ClientMethod · 0.95
OpenMethod · 0.80
UploadMethod · 0.80
StringMethod · 0.80
NewMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected