go操作aws s3

v2 官方推荐版本,需要go版本>=1.20 安装 go get github.com/aws/aws-sdk-go-v2 go get github.com/aws/aws-sdk-go-v2/config go get github.com/aws/aws-sdk-go-v2/service/s3 必要参数 bucket: 存储桶的名称 Region: 存储桶所在区域,例us-east-1 accessKeyID: 访问存储桶所需的KeyID secretAccessKey: 访问存储桶所需的AccessKey 如何获取accessKeyID和secretAccessKey:指南 var ( s3Client *s3.Client bucket = "yourbucketname" ) func InitS3Client() { accessKeyID := "xxxxx" secretAccessKey := "xxxxx" cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("ap-east-1"), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyID, secretAccessKey, "")), ) if err != nil { log.Fatalf("unable to load SDK config, %v", err) } s3Client = s3.NewFromConfig(cfg) } func UploadToS3(filePath, fileName, mimeType string) (string, error) { file, err := os.Open(filePath) if err != nil { return "", fmt.Errorf("failed to open file %q, %v", filePath, err) } defer file.Close() _, err = s3Client.PutObject(context.TODO(), &s3.PutObjectInput{ Bucket: aws.String(bucket), Key: aws.String(fileName), Body: file, ContentType: aws.String(mimeType), }) if err != nil { return "", fmt.Errorf("failed to upload file to S3, %v", err) } return fmt.Sprintf("https://%s.s3.amazonaws.com/%s", bucket, fileName), nil } 参考2 // 创建s3Client实例。 payKeyId := "AK***" paySecret := "oiy***" accessKeyID := payKeyId secretAccessKey := paySecret cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("ap-east-1"), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyID, secretAccessKey, "")), ) if err != nil { fmt.Printf("unable to load SDK config, %v", err) } name := "test" s3Client := s3.NewFromConfig(cfg) filePath := "static/test.jpg" // 读取本地文件。 fd, err := os.Open(filePath) if err != nil { return } defer fd.Close() _, err = s3Client.PutObject(context.TODO(), &s3.PutObjectInput{ Bucket: aws.String("up"), Key:

gin获取请求体(json格式)参数

gin 框架里 c.PostForm() 函数只能从表单中获取参数,不能从 body 中解析表单参数,也就是说你如果用 c.PostForm() 来解析获取参数,客户端发起请求时,如果参数放在表单(form-data)里,服务端能正常获取到参数,但是如果客户端把参数放在 raw 里,即使 header 里配置了 content-type:multipart/form-data,服务端仍就无法获取到参数。 c.PostForm获取表单数据 package main import ( "bytes" "fmt" "github.com/gin-gonic/gin" "io/ioutil" ) func main() { gin.SetMode(gin.ReleaseMode) r := gin.Default() r.GET("/test", func(c *gin.Context) { data, _ := ioutil.ReadAll(c.Request.Body) fmt.Printf("req.body=%s\n, content-type=%v\n", data, c.ContentType()) // 这点很重要,把字节流重新放回 body 中 c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(data)) // 获取参数 userName := c.PostForm("user_name") age := c.PostForm("age") fmt.Printf("userName=%s, age=%s\n", userName, age) c.JSON(200, "success") }) r.Run(":7000") } 或 func Test(c *gin.Context) error { m := make(mapinterface{}) m = c.Request.PostFormValue("memberid") // 商户ID m = c.Request.PostFormValue("orderid") // 订单号 } 从body中获取参数 package main import ( "bytes" "encoding/json" "fmt" "github.com/gin-gonic/gin" "io" "io/ioutil" ) type User struct { UserName string `json:"user_name"` Age int `json:"age"` } // 关键 func JSONDecode(r io.Reader, obj interface{}) error { if err := json.NewDecoder(r).Decode(obj); err != nil { return err } return nil } func main() { gin.SetMode(gin.ReleaseMode) r := gin.Default() r.POST("/test", func(c *gin.Context) { // 打印出 body //data, _ := ioutil.ReadAll(c.Request.Body) //fmt.Printf("req.body=%s\n, content-type=%v\n", data, c.ContentType()) var json User data, err := c.GetRawData() if err != nil { fmt.Println(err.Error()) } fmt.Println("data:", string(data)) // 把字节流重新放回 body 中 c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(data)) err2 := JSONDecode(c.Request.Body, &json) if err2 != nil { fmt.Println("decode err:", err2) } // 打印body中的参数 fmt.Println("user_name:", json.UserName, "age:", json.Age) c.JSON(200, "success") }) r.Run(":7000")