golang oss文件上传(服务端上传文件到oss)
type OSS struct {
AccessKey string `yaml:"accessKey" json:"accessKey"`
Secret string `yaml:"secret" json:"secret"`
Domain string `yaml:"domain" json:"domain"`
Bucket string `yaml:"bucket" json:"bucket"`
AccessName string `yaml:"accessName" json:"acessName"`
}
func NewOSS(viper *viper.Viper) (*OSS, error) {
var config = new(OSS)
if err := viper.UnmarshalKey("ossConfig", config); err != nil {
fmt.Println("读取oss错误", err)
}
fmt.Println("oss数据", config)
return config, nil
}
func (this OSS) Upload(file io.Reader, ossPath string) (string, error) {
client, err := oss.New(this.Domain, this.AccessKey, this.Secret)
if err != nil {
return "", err
}
bucket, err := client.Bucket(this.Bucket)
if err != nil {
return "", err
}
err = bucket.PutObject(ossPath, file)
if err != nil {
return "", err
}
fmt.Println("处理前", ossPath)
return this.AccessName + `/` + ossPath, nil
}