package utils import ( "adm-gateway/parser" "crypto/md5" "encoding/base64" "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) const ( ossId = "LTAI4Fjs1bNMd8k8LT41Fxxn" ossSecret = "WlQJwVbxHIY4TIZwMIFA5QCye6n0Pl" ossEndpoint = "oss-cn-shanghai.aliyuncs.com" ossBucket = "gd-image" ) func Base64Encode(src []byte) []byte { return []byte(base64.StdEncoding.EncodeToString(src)) } func Base64Decode(src string) ([]byte, error) { return base64.StdEncoding.DecodeString(src) } func MD5(src string) string { h := md5.New() h.Write([]byte(src)) re := h.Sum(nil) return fmt.Sprintf("%x", re) } func isOssConfigured() bool { if parser.Conf.Oss.EndPoint == "" || parser.Conf.Oss.Bucket == "" || parser.Conf.Oss.AccessKey == "" || parser.Conf.Oss.AccessSecret == "" { return false } return true } // OssGetUrl 获取oss下载地址url前缀 func OssGetUrl() string { if isOssConfigured() == false { return "https://" + ossBucket + "." + ossEndpoint } return "https://" + parser.Conf.Oss.Bucket + "." + parser.Conf.Oss.EndPoint } func ossGetInfo() (id, secret, bucket, endpoint string) { id = ossId secret = ossSecret bucket = ossBucket endpoint = ossEndpoint if isOssConfigured() { id = parser.Conf.Oss.AccessKey secret = parser.Conf.Oss.AccessSecret bucket = parser.Conf.Oss.Bucket endpoint = parser.Conf.Oss.EndPoint } return id, secret, bucket, endpoint } // OssUploadFile 上传文件到oss服务器 func OssUploadFile(path string, fileName string) error { id, secret, buck, endpoint := ossGetInfo() client, err := oss.New(endpoint, id, secret) if err != nil { return err } // 获取存储空间。 bucket, err := client.Bucket(buck) if err != nil { return err } err = bucket.PutObjectFromFile(fileName, path) return err }