oss config

This commit is contained in:
2023-04-07 04:15:50 +08:00
parent b20e864b37
commit dc92631a73
5 changed files with 41 additions and 3 deletions

View File

@@ -29,6 +29,6 @@ func 生成配置文件() {
viper.Set("mysql.maxOpenConns", 100)
viper.Set("oss.host", "")
viper.Set("oss.accessKeyId", "")
viper.Set("oss.accessKeySecret", "")
viper.Set("oss.accessId", "")
viper.Set("oss.accessKey", "")
}

32
models/oss.go Normal file
View File

@@ -0,0 +1,32 @@
package models
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func GetBucket(bucketName string) *oss.Bucket {
// 从config文件中读取配置
endpoint := Viper.Get("oss.endpoint").(string)
accessID := Viper.Get("oss.accessID").(string)
accessKey := Viper.Get("oss.accessKey").(string)
client, err := oss.New(endpoint, accessID, accessKey)
if err != nil {
HandleError(err)
}
bucket, err := client.Bucket(bucketName)
if err != nil {
HandleError(err)
}
return bucket
}
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}