oss config
This commit is contained in:
		@@ -25,7 +25,7 @@ func init() {
 | 
			
		||||
// 实现一个 web api 服务(获取指定尺寸的图片)
 | 
			
		||||
func main() {
 | 
			
		||||
	runtime.GOMAXPROCS(runtime.NumCPU())
 | 
			
		||||
	models.GetMysql() // 连接数据库
 | 
			
		||||
	models.GetMysql() // 测试连接数据库
 | 
			
		||||
 | 
			
		||||
	http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@@ -19,12 +19,14 @@ require (
 | 
			
		||||
	github.com/subosito/gotenv v1.4.2 // indirect
 | 
			
		||||
	golang.org/x/sys v0.3.0 // indirect
 | 
			
		||||
	golang.org/x/text v0.5.0 // indirect
 | 
			
		||||
	golang.org/x/time v0.1.0 // indirect
 | 
			
		||||
	gopkg.in/ini.v1 v1.67.0 // indirect
 | 
			
		||||
	gopkg.in/yaml.v2 v2.4.0 // indirect
 | 
			
		||||
	gopkg.in/yaml.v3 v3.0.1 // indirect
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible
 | 
			
		||||
	github.com/casbin/casbin/v2 v2.66.2
 | 
			
		||||
	github.com/chai2010/webp v1.1.1
 | 
			
		||||
	github.com/go-sql-driver/mysql v1.7.0
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							@@ -39,6 +39,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
 | 
			
		||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 | 
			
		||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
 | 
			
		||||
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
 | 
			
		||||
github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible h1:KpbJFXwhVeuxNtBJ74MCGbIoaBok2uZvkD7QXp2+Wis=
 | 
			
		||||
github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
 | 
			
		||||
github.com/casbin/casbin/v2 v2.66.2 h1:C4qtZCs5z3tV6iq92lO9ud/bLdePgbIstFKIiEvcOcA=
 | 
			
		||||
github.com/casbin/casbin/v2 v2.66.2/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
 | 
			
		||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
 | 
			
		||||
@@ -325,6 +327,8 @@ golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
 | 
			
		||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
 | 
			
		||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
 | 
			
		||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
 | 
			
		||||
golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA=
 | 
			
		||||
golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
 | 
			
		||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 | 
			
		||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 | 
			
		||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
 | 
			
		||||
 
 | 
			
		||||
@@ -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
									
								
							
							
						
						
									
										32
									
								
								models/oss.go
									
									
									
									
									
										Normal 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)
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user