123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package oss
- import (
- "bytes"
- "crypto/tls"
- "eta_gn/eta_obs/global"
- "fmt"
- "github.com/aws/aws-sdk-go/aws"
- "github.com/aws/aws-sdk-go/aws/credentials"
- "github.com/aws/aws-sdk-go/aws/session"
- "github.com/aws/aws-sdk-go/service/s3"
- "io/ioutil"
- "net/http"
- "time"
- )
- type S3Oss struct{}
- func (m *S3Oss) UploadFile(fileName, localFile, savePath string) (resourceUrl string, err error) {
- defer func() {
- if err != nil {
- fmt.Println(err.Error())
- }
- }()
- // 默认使用后端这个, 这里有两个配置的原因是
- // 前端上传跨域问题可能会使用反向代理来解决, 这样的话同一个endpoint就会导致一端正常另一端不正常
- endpoint := global.CONFIG.Oss.BackEndPoint
- if endpoint == "" {
- endpoint = global.CONFIG.Oss.EndPoint
- }
- accessKey := global.CONFIG.Oss.AccessKeyId
- secretKey := global.CONFIG.Oss.AccessKeySecret
- region := global.CONFIG.Oss.RegionId
- bucketName := global.CONFIG.Oss.BucketName
- uploadDir := global.CONFIG.Oss.UploadDir
- config := &aws.Config{
- Region: aws.String(region),
- Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
- Endpoint: aws.String(endpoint),
- S3ForcePathStyle: aws.Bool(global.CONFIG.Oss.ForceStyle),
- DisableSSL: aws.Bool(global.CONFIG.Oss.DisableSSL),
- }
- if global.CONFIG.Oss.DisableSSL {
- config.HTTPClient = &http.Client{
- Transport: &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
- },
- }
- }
- //b, _ := json.Marshal(config)
- //fmt.Println(string(b))
- // 创建AWS会话
- sess, e := session.NewSession(config)
- if e != nil {
- err = fmt.Errorf("new session err: %s", e.Error())
- return
- }
- // 创建S3服务客户端
- client := s3.New(sess)
- // 读取文件内容
- fileContent, e := ioutil.ReadFile(localFile)
- if e != nil {
- err = fmt.Errorf("read file err: %s", e.Error())
- return
- }
- path := savePath
- if savePath == "" {
- path = uploadDir + time.Now().Format("200601/20060102/") + fileName
- }
- putObjectInput := &s3.PutObjectInput{
- Bucket: aws.String(bucketName),
- Key: aws.String(path),
- Body: bytes.NewReader(fileContent),
- }
- if global.CONFIG.Oss.OpenAcl {
- putObjectInput.ACL = aws.String(s3.ObjectCannedACLPublicRead)
- }
- fmt.Printf("put object input: %+v\n", putObjectInput)
- _, e = client.PutObject(putObjectInput)
- if e != nil {
- err = fmt.Errorf("put object err: %s", e.Error())
- return
- }
- resourceUrl = global.CONFIG.Oss.ImgHost + path
- if global.CONFIG.Oss.ResourceProxyUrl != "" {
- resourceUrl = global.CONFIG.Oss.ResourceProxyUrl + path
- }
- return
- }
- func (m *S3Oss) GetUploadToken() (token OssToken, err error) {
- token.Endpoint = global.CONFIG.Oss.EndPoint
- token.AccessKeyId = global.CONFIG.Oss.AccessKeyId
- token.AccessKeySecret = global.CONFIG.Oss.AccessKeySecret
- token.RegionId = global.CONFIG.Oss.RegionId
- token.Bucketname = global.CONFIG.Oss.BucketName
- token.ImgHost = global.CONFIG.Oss.ImgHost
- token.Port = global.CONFIG.Oss.Port
- token.S3ForceStyle = global.CONFIG.Oss.ForceStyle
- token.S3Protocol = global.CONFIG.Oss.Protocol
- return
- }
|