初始化SDK 设置serviceclient 使用SDK访问对象存储服务,首先需要提供正确的AccessKey和SecretAccessKey以及服务端地址EndPoint,用于设置一个service client。 创建session 配置service client首先需要创建一个session, session包含了service client的配置信息,例如AccessKey、SecretAccessKey以及发送请求的附加信息等。一个session可以被用于创建多个service client。 代码示例: plaintext import ( "github.com/aws/awssdkgo/aws" "github.com/aws/awssdkgo/aws/credentials" "github.com/aws/awssdkgo/aws/session" "github.com/aws/awssdkgo/service/s3" ) const ( Endpoint " " AccessKey " " SecretAccessKey " " ) // BuildSession 创建并返回一个session func BuildSession() session.Session { conf : &aws.Config{ Endpoint: aws.String(Endpoint), // Set this to true to force the request to use pathstyle addressing, // i.e., By default, the S3 client // will use virtual hosted bucket addressing when possible // ( S3ForcePathStyle: aws.Bool(false), //Set this to true to disable SSL when sending requests. Defaults to false DisableSSL: aws.Bool(true), Credentials: credentials.NewStaticCredentials(AccessKey, SecretAccessKey, ""), LogLevel: aws.LogLevel(aws.LogDebug)} sess : session.Must(session.NewSessionWithOptions(session.Options{Config: conf})) return sess } 创建service client 代码示例: plaintext // BuildClient 创建并返回一个service client func BuildClient() s3.S3 { conf : &aws.Config{ Endpoint: aws.String(Endpoint), S3ForcePathStyle: aws.Bool(false), DisableSSL: aws.Bool(true), Credentials: credentials.NewStaticCredentials(AccessKey, SecretAccessKey, ""), LogLevel: aws.LogLevel(aws.LogDebug)} sess : session.Must(session.NewSessionWithOptions(session.Options{Config: conf})) svc : s3.New(sess) return svc }