gpt4 book ai didi

amazon-web-services - 我是否必须在每个api请求中将Session和S3定义为包变量或创建实例

转载 作者:行者123 更新时间:2023-12-03 10:10:46 25 4
gpt4 key购买 nike

我是否必须在每个api request中将Session和S3定义为包变量或创建实例。示例代码如下。非常感谢。

var AWSSession *session.Session
var S3Client *s3.S3

func InitAws() error {

log.Info("InitAws")

AWSSession, err := session.NewSession(&aws.Config{
Region: aws.String("XXXX"),
Credentials: credentials.NewStaticCredentials( "XX","XX",""), //just for test
})

if err != nil {
return err
}

log.Info("InitAws S3 Client")
S3Client = s3.New(AWSSession)

log.Info("InitAws end")
return nil
}

最佳答案

package documentation says:

Sessions are safe to use concurrently as long as the Session is not being modified. Sessions should be cached when possible, because creating a new Session will load all configuration values from the environment, and config files each time the Session is created. Sharing the Session value across all of your service clients will ensure the configuration is loaded the fewest number of times possible.


不要创建一个 session 并重用它。
您是将单个 session 存储在程序包级别的变量中,还是将其传递给函数参数,这是一个带有问题答案的独立问题。

关于amazon-web-services - 我是否必须在每个api请求中将Session和S3定义为包变量或创建实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65325694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com