AWS3文件上传 golang 客户端
package utils
import (
"bufio"
"bytes"
"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"
tsgutils "github.com/typa01/go-utils"
)
type AmazonClient struct {
Region string
AccessKeyID string // id
SecretKey string // secret
BucketName string
UrlMain string
UrlFolder string // format "/operation/upload/jinhaiyun/"
FileType string
//options
Token string
ACL string
ContentType string
// operatte outcome
Session *session.Session
FilePath string // after upload, this file path will exist , which is the internal path, not include bucket
}
type ClientOpt struct {
//ACL: 权限枚举 https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl
ACL string
ContentType string
Token string
}
type Option func(ops *ClientOpt)
//For access controller
func WithAcl(acl string) Option {
return func(ops *ClientOpt) {
ops.ACL = acl
}
}
func WithContentType(contentType string) Option {
return func(ops *ClientOpt) {
ops.ContentType = contentType
}
}
func WithToken(token string) Option {
return func(ops *ClientOpt) {
ops.Token = token
}
}
func NewAmazonClient(region, accessKeyID, secretKey, bucketName, urlMain, urlFolder,fileType string, options ...Option) *AmazonClient {
optionsItem := new(ClientOpt)
for i := range options {
options[i](optionsItem)
}
client := new(AmazonClient)
client.ACL = optionsItem.ACL
client.ContentType = optionsItem.ContentType
client.Token = optionsItem.Token
client.Region = region
client.AccessKeyID = accessKeyID
client.SecretKey = secretKey
client.BucketName = bucketName
client.UrlMain = urlMain
client.UrlFolder = urlFolder
client.FileType = fileType
sess, err := session.NewSession(&aws.Config{
Region: aws.String(client.Region), //桶所在的区域
Credentials: credentials.NewStaticCredentials(client.AccessKeyID, client.SecretKey, client.Token), //sts的临时凭证
})
if err != nil {
panic(err)
}
client.Session = sess
return client
}
func (this *AmazonClient) Upload(buffer []byte) (url string, err error) {
filePath := this.UrlFolder + tsgutils.GUID() + this.FileType
this.FilePath=filePath
_, err = s3.New(this.Session).PutObject(&s3.PutObjectInput{
Bucket: aws.String(this.BucketName), //桶名
Key: aws.String(filePath), //文件的目录+文件名
Body: bytes.NewReader(buffer),
ACL: aws.String("public-read"),
//ContentType: aws.String("image/jpeg"),
})
if err != nil {
panic(err.Error())
}
url = this.UrlMain + filePath
return
}
func (this *AmazonClient) Download() (fileByte []byte, err error) {
out, err := s3.New(this.Session).GetObject(&s3.GetObjectInput{
Bucket: aws.String("moole-verify-img-bucket-dev"), //桶名
Key: aws.String(this.FilePath),
})
defer out.Body.Close()
if err != nil {
panic(err.Error())
}
scanner := bufio.NewScanner(out.Body)
for scanner.Scan() {
fileByte = scanner.Bytes()
}
return
}
package common_use
import (
"api_quant_manage_system/utils"
"github.com/spf13/viper"
"sync"
)
var xlsAWSClient *utils.AmazonClient
func InitxlsClient(v *viper.Viper)*utils.AmazonClient{
if xlsAWSClient ==nil{
once:=sync.Once{}
once.Do(func() {
if xlsAWSClient ==nil{
xlsAWSClient =utils.NewAmazonClient(
v.GetString("region"),
v.GetString("access_key_id"),
v.GetString("secret_key"),
v.GetString("bucket_name"),
v.GetString("url_main"),
v.GetString("url_folder"),
v.GetString("file_type"),
utils.WithToken(v.GetString("token")),
utils.WithAcl(v.GetString("acl")),
utils.WithAcl(v.GetString("content_type")),
)
}
})
}
return xlsAWSClient
}
func GetXlsAwsClient() *utils.AmazonClient {
return xlsAWSClient
}