package aws import ( "context" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/s3" "hilo/common/httpclient" "hilo/common/util" //"hilo/biz/util" //hiloCfg "hilo/common/config" //"hilo/common/mylogrus" "log" "mime/multipart" ) var Global *Aws type ( Config struct { Bucket string Cdn string Dir string } Aws struct { Config *Config Cfg aws.Config } ) func NewAWS(conf *Config) *Aws { // Load the Shared AWS Configuration (~/.aws/config) a := &Aws{ Config: conf, } var err error = nil cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { log.Fatal(err) } a.Cfg = cfg return a } func SetAws(a *Aws) { Global = a } func (a *Aws) UploadImage(imagPath string) (string, error) { resp, _ := httpclient.Default.Get(imagPath) // Create an Amazon S3 service client client := s3.NewFromConfig(a.Cfg) fileName := a.Config.Dir + "image/" + util.GetRandStrByUUID() ct := "image/jpeg" _, err := client.PutObject(context.TODO(), &s3.PutObjectInput{ Bucket: aws.String(a.Config.Bucket), Key: aws.String(fileName), Body: resp.Body, ContentLength: resp.ContentLength, ContentType: &ct, }) if err != nil { return "", err } return fileName, nil } func (a *Aws) UploadImageFile(imagFile multipart.File, t string, contentType *string) (string, error) { // Create an Amazon S3 service client client := s3.NewFromConfig(a.Cfg) fileName := a.Config.Dir + "manager/" + t _, err := client.PutObject(context.TODO(), &s3.PutObjectInput{ Bucket: aws.String(a.Config.Bucket), Key: aws.String(fileName), Body: imagFile, ContentType: contentType, }) if err != nil { return "", err } return fileName, nil }