登录
首页 >  Golang >  Go问答

AWS SDK会自动处理会话超时吗?

来源:stackoverflow

时间:2024-02-19 08:06:23 236浏览 收藏

大家好,今天本人给大家带来文章《AWS SDK会自动处理会话超时吗?》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

问题内容

我正在编写一个客户端来从 rabbitmq 获取连续消息并将其推送到 aws sqs 服务。但我不确定会话是否过期,如果会话过期,我们是否需要重新创建会话或 aws sdk 会自动处理它?

log.Printf("PPU Message Broker: Pushing messages to SQS")
    sess, err := session.NewSession(&aws.Config{
        Region:      aws.String("us-east-1"),
        //Credentials: credentials.NewSharedCredentials("", "sqs_user"),
    })
    _, err = sess.Config.Credentials.Get()
    if err != nil {
        log.Fatalf("PPU Message Broker: Credentails Failed")
    }
    svc := sqs.New(sess)    
    result, err := svc.SendMessage(&sqs.SendMessageInput{
        MessageBody:    aws.String(string(data)),
        MessageGroupId: aws.String("TestGroup"),
        QueueUrl:       &qURL,
    })

解决方案


会话过期有一个默认配置,但您可以指定您的:

除了 newsession 之外,您还可以使用以下命令创建会话 newsessionwithoptions。此功能允许您控制和 覆盖如何通过代码创建会话,而不是 仅由环境变量驱动。

当您想要提供配置文件时,请使用 newsessionwithoptions

在options对象内部,有一个属性可以改变默认的过期时间,默认是15分钟:

// 当 sdk 的共享配置配置为承担此角色时 选项 // 可以设置 sts 凭证的过期时间。 // 如果未按照文档中的记录进行设置,则默认为 15 分钟 // stscreds.assumeroleprovider。 assumeroleduration 时间.duration

https://docs.aws.amazon.com/sdk-for-go/api/aws/session/

更新的答案:

刚刚注意到问题是关于适用于 go 的 aws sdk。

来自 aws sdk for go 文档:https://docs.aws.amazon.com/sdk-for-go/api/aws/credentials/#Credentials.Get

func (*credentials) get

func (c *credentials) get() (value, error)

get returns the credentials value, or error if the credentials value failed to be retrieved.

will return the cached credentials value if it has not expired. if the credentials value has expired the provider's retrieve() will be called to refresh the credentials.

if credentials.expire() was called the credentials value will be force expired, and the next call to get() will cause them to be refreshed.

原答案:

来自 aws javascript sdk 文档(https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Credentials.html#get-property

get(callback) ⇒ void

Gets the existing credentials, refreshing them if they are not yet loaded or have expired. Users should call this method before using refresh(), as this will not attempt to reload credentials when they are already loaded into the object.

今天关于《AWS SDK会自动处理会话超时吗?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>