AWS S3会话是否会过期?

huangapple go评论91阅读模式
英文:

Does an AWS S3 session expire?

问题

我有一个从S3存储桶下载文件的服务。

为此,我需要一个*session.Session

我看到有两个选项。

选项1

type S3Client struct {
    session *session.Session
}

func NewS3Client(region string) (*S3Client, error) {
    sess, err := session.NewSession(&aws.Config{Region: aws.String(region)})
    if err != nil {
        return nil, err
    }

    return &S3Client{
        session: sess,
    }, nil
}

然后,我可以使用给定的会话使用客户端下载文件。

选项2

每次需要下载文件时,打开一个新的会话。

func DownloadFromS3(region, bucket, file string) (string, error) {
    sess, err := session.NewSession(&aws.Config{Region: aws.String(region)})
    if err != nil {
        return "", err
    }

    // 使用会话下载文件


问题:选项1是否可行,或者会话是否有一种过期机制?

英文:

I have a service that downloads files from an s3 bucket.

For that I need a *session.Session.

I see myself with two options.

Option 1

type S3Client struct {
    session *session.Session
}

func NewS3Client(region string) (*S3Client, error) {
    sess, err := session.NewSession(&aws.Config{Region: aws.String(region)})
    if err != nil {
        return nil, err
    }

    return &S3Client{
        session: sess,
    }, nil
}

Then I may use the client to download stuff with given session.

Option 2

Open a new session every time I need to download a file.

func DownloadFromS3(region, bucket, file string) (string, error) {
	sess, err := session.NewSession(&aws.Config{Region: aws.String(region)})
	if err != nil {
		return "", err
	}

    // download using the session


Question: is option 1 viable or is there a sort of expiration mechanism on the session ?

答案1

得分: 2

session这个名称是误导性的。在AWS SDK for Go V2中,它被一个简化的配置系统所取代,该系统由config包提供。请参阅Configuration Loading

session包的文档明确指出,如果可能的话,应该缓存会话:

session包为SDK的服务客户端提供配置。会话可以在共享相同基本配置的服务客户端之间共享。

只要不修改会话,会话就可以安全地并发使用。应尽可能缓存会话,因为每次创建新会话时,都会从环境和配置文件中加载所有配置值。通过在所有服务客户端之间共享会话值,可以确保尽可能少地加载配置。

英文:

The name session is misleading. It is replaced with a simplified configuration system provided by the config package in AWS SDK for Go V2. See Configuration Loading.

The document for the session package explicitly states that sessions should be cached if possible:

> Package session provides configuration for the SDK's service clients. Sessions can be shared across service clients that share the same base configuration.
>
> 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.

huangapple
  • 本文由 发表于 2023年4月26日 18:38:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76109802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定