oss.go
743 字节
package oss
import (
"bytes"
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/linmadan/egglib-go/utils/snowflake"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
)
var snow, _ = snowflake.NewIdWorker(1)
func UploadOss(data []byte) (string, error) {
client, err := oss.New("oss-cn-shenzhen.aliyuncs.com", constant.OssAccessKey, constant.OssSecret)
if err != nil {
return "", err
}
bucket, err := client.Bucket(constant.OssBucket)
if err != nil {
return "", err
}
fileId, err := snow.NextId()
if err != nil {
return "", err
}
fileName := "costs/" + fmt.Sprintf("%v", fileId)
err = bucket.PutObject(fileName, bytes.NewBuffer(data))
return constant.OssDomain +"/"+ fileName, err
}