package oss import ( ossDomain "erpims/application/domain/oss" "erpims/config" "erpims/utils" "errors" "git.sxidc.com/service-supports/fslog" "github.com/aliyun/aliyun-oss-go-sdk/oss" "io" "strings" ) var bkt *oss.Bucket var pfx string func Init(endpoint, keyID, keySecret, prefix string) error { pfx = prefix client, err := oss.New(endpoint, keyID, keySecret) if err != nil { return err } bkt, err = client.Bucket("fs-yf") if err != nil { return err } return nil } func Touch(reader io.Reader, to string) error { if reader == nil || utils.HasBlank(to) { fslog.Warn("oss上传发现违规参数") return nil } return bkt.PutObject(pfx+to, reader) } func Url(obj string) (string, error) { if utils.HasBlank(obj) { fslog.Warn("oss获取Url发现违规参数") return "", nil } return bkt.SignURL(pfx+obj, oss.HTTPGet, 60) } func UrlWithoutPrefix(obj string) (string, error) { if utils.HasBlank(obj) { fslog.Warn("oss获取Url发现违规参数") return "", nil } return bkt.SignURL(obj, oss.HTTPGet, 60) } func ZoomUrl(obj, s string) (string, error) { if utils.HasBlank(obj) { fslog.Warn("oss发现违规参数") return "", nil } return bkt.SignURL(pfx+obj, oss.HTTPGet, 60, oss.Process(s)) } func Exist(obj string) (bool, error) { if utils.HasBlank(obj) { return false, nil } return bkt.IsObjectExist(pfx + obj) } func ExistWithoutPrefix(obj string) (bool, error) { if utils.HasBlank(obj) { return false, nil } return bkt.IsObjectExist(obj) } func Rm(objs ...string) error { L := len(objs) if L == 0 { return nil } result, err := bkt.DeleteObjects(objs) if err != nil { return err } if len(result.DeletedObjects) != L { go func() { // 日志打印哪些Obj删除失败 sucMap := make(map[string]struct{}, len(result.DeletedObjects)) for _, deleted := range result.DeletedObjects { sucMap[deleted] = struct{}{} } for _, obj := range objs { if _, ok := sucMap[obj]; !ok { fslog.Warn("Oss删除%s图片失败") } } }() } return nil } func Ls(dir string) ([]string, error) { if utils.HasBlank(dir) { return []string{}, nil } v2, err := bkt.ListObjectsV2(oss.Prefix(pfx + dir)) if err != nil { return nil, err } ks := make([]string, len(v2.Objects)) for i, obj := range v2.Objects { ks[i] = obj.Key } return ks, nil } func Mv(f, t string) error { if err := Cp(f, t); err != nil { return err } return bkt.DeleteObject(pfx + f) } func Cp(f, t string) error { _, err := bkt.CopyObject(pfx+f, pfx+t) if err != nil { return err } return nil } func MvWithoutPrefix(f, t string) error { if _, err := bkt.CopyObject(f, t); err != nil { return err } return bkt.DeleteObject(f) } func MultipleFileMv(filePath string) (string, error) { endPicture := make([]string, 0) files := strings.Split(filePath, ",") for _, file := range files { //文件名 tmpFx := config.GetConfig().OssConfig.TmpPrefix if utils.HasText(file) && strings.Contains(file, "/temp/") { fileName, found := strings.CutPrefix(file, tmpFx) if !found { return "", errors.New("文件名处理失败") } err := MvWithoutPrefix(file, pfx+fileName) if err != nil { return "", errors.New("文件移动失败") } endPicture = append(endPicture, pfx+fileName) continue } endPicture = append(endPicture, file) } return strings.Join(endPicture, ","), nil } func MultipleFileUrl(filePath string) ([]ossDomain.FileObject, error) { filePathArr := strings.Split(filePath, ",") picture := make([]ossDomain.FileObject, 0) for _, file := range filePathArr { exist, err := ExistWithoutPrefix(file) if err != nil { fslog.Error(err) picture = append(picture, ossDomain.FileObject{ Url: file, Temp: "", }) continue } if !exist { fslog.Error("文件【" + file + "】不存在") picture = append(picture, ossDomain.FileObject{ Url: file, Temp: "", }) continue } url, err := UrlWithoutPrefix(file) if err != nil { fslog.Error("文件【" + file + "】链接生成失败") picture = append(picture, ossDomain.FileObject{ Url: file, Temp: "", }) continue } name, _ := strings.CutPrefix(file, pfx+"/") picture = append(picture, ossDomain.FileObject{ Url: file, Name: name, Temp: url, }) } return picture, nil }