package utils import ( "fmt" "github.com/google/uuid" "github.com/qiniu/go-sdk/v7/auth/qbox" "github.com/qiniu/go-sdk/v7/storage" "log" "project_management/global" "strconv" "time" ) // GetToken 获取上传凭证 func GetToken(T_suffix string) string { mac := qbox.NewMac(global.QiniuSetting.AccessKeyID, global.QiniuSetting.AccessKeySecret) Tokey := strconv.FormatInt(time.Now().Unix(), 10) + uuid.NewString() if len(T_suffix) == 0 { T_suffix = "png" } // 构建表单上传的对象 putPolicy := storage.PutPolicy{ Scope: global.QiniuSetting.BucketName, Expires: 7200, InsertOnly: 1, ForceSaveKey: true, ReturnBody: `{"key":"` + global.QiniuSetting.Endpoint + `/$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)"}`, SaveKey: "UpImage/" + Tokey + "." + T_suffix, MimeLimit: "image/*;application/pdf;application/octet-stream;application/zip;application/x-tar", FsizeLimit: 1024 * 1024 * 100, } upToken := putPolicy.UploadToken(mac) return upToken } // DeleteFiles 删除文件 func DeleteFiles(accessKey, secretKey, bucket string, files []string) { cfg := storage.Config{ Zone: &storage.ZoneHuanan, } bucketManager := storage.NewBucketManager(qbox.NewMac(accessKey, secretKey), &cfg) ops := make([]string, len(files)) for i, file := range files { ops[i] = storage.URIDelete(bucket, file) } _, err := bucketManager.Batch(ops) if err != nil { log.Fatal("Batch delete failed:", err) } fmt.Println("Files deleted successfully.") } // GetFileInfo 获取文件信息 func GetFileInfo(accessKey, secretKey, bucket string, files []string) map[string]storage.FileInfo { mac := qbox.NewMac(accessKey, secretKey) cfg := storage.Config{ Zone: &storage.ZoneHuadong, } bucketManager := storage.NewBucketManager(mac, &cfg) fileInfoMap := make(map[string]storage.FileInfo) for _, file := range files { fileInfo, err := bucketManager.Stat(bucket, file) if err != nil { log.Println("Get file info failed:", err) } else { fileInfoMap[file] = fileInfo } } return fileInfoMap }