英文:
cannot use &cfg (value of type *aws.Config) as *aws.Config value in argument to session.NewSession
问题
我正在尝试这样获取一个新的AWS会话:
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatal(err)
}
awsSession, err := session.NewSession(&cfg)
if err != nil {
log.Fatal(err)
}
但是我得到了一个令人困惑的错误(至少对我来说):
无法将
&cfg
(类型为*aws.Config
的值)作为session.NewSession
的参数中的*aws.Config
值使用
NewSession()
的签名如下:
func NewSession(cfgs ...*aws.Config) (*Session, error)
NewSession()
是一个可变参数函数,这个事实是否在产生这个错误时起了作用?
为什么如果我像这样传递一个字面量aws.Config{}
,错误就消失了?
awsSession, err := session.NewSession(&aws.Config{})
英文:
I'm trying to get a new AWS Session like this:
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatal(err)
}
awsSession, err := session.NewSession(&cfg)
if err != nil {
log.Fatal(err)
}
But I get a confusing (at least to me) error:
> cannot use &cfg (value of type *aws.Config) as *aws.Config value in argument to session.NewSession
The signature of NewSession()
looks like this:
func NewSession(cfgs ...*aws.Config) (*Session, error)
Does the fact that NewSession()
is a variadic function play any role in producing this error?
Why does the error go away if I pass a literal aws.Config{}
like this?
awsSession, err := session.NewSession(&aws.Config{})
答案1
得分: 3
你混淆了AWS SDK的版本。
session
包存在于AWS SDK v1中,导入路径为github.com/aws/aws-sdk-go/aws/session
,并且期望类型为"github.com/aws/aws-sdk-go/aws".Config
的参数。它在AWS SDK v2中不存在。
你加载配置的config
包来自于AWS SDK v2,导入路径为github.com/aws/aws-sdk-go-v2/config
,因此LoadDefaultConfigs
返回的是"github.com/aws/aws-sdk-go-v2/config".Config
。
这两个类型不同,编译器正在告诉你这一点(尽管错误消息只有部分有用)。
你必须保持AWS SDK版本的一致性。在v2中,你应该使用配置来实例化特定的AWS服务,而不是使用session。例如s3.New(&cfg)
。
英文:
You're mixing up AWS SDK versions.
The package session
exists in AWS SDK v1 with import path github.com/aws/aws-sdk-go/aws/session
and expects arguments of type "github.com/aws/aws-sdk-go/aws".Config
. It does not exist in AWS SDK v2.
The config
package you were loading your configs from instead is from AWS SDK v2 with import path github.com/aws/aws-sdk-go-v2/config
therefore LoadDefaultConfigs
returns "github.com/aws/aws-sdk-go-v2/config".Config
.
These are not the same type and the compiler is telling you that (albeit with a partially useful error message).
You have to keep your AWS SDK versions consistent. With v2, you are supposed to instantiate the specific AWS services with the config, instead of a session. For example s3.New(&cfg)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论