C# 在哪里可以使用无参数构造函数获取 AWS 凭证
Where can C# get AWS credentials with parameterless constructor
我目前正在维护一个由另一个团队编写的项目,有些地方我一直无法理解。
他们集成了 测试 连接到 AWS 资源,如 Dynamo、S3 等。在他们的代码中,他们使用无参数构造函数,显然测试使用默认凭据运行。
例如:
new AmazonDynamoDBClient();
如何实现与默认构造函数的连接?有人可以帮我吗?
注意:暂时不能改成依赖注入模型
对于本地开发,凭据是从 shared config and credentials file which on your local machine can be set up by using the aws configure
cli command with an AWS Access Key. The aws configure
command will ask for your access key ID, Secret token and a default region code 加载的。这 3 个是无参数构造函数工作所必需的,第四个参数 Default output format
是可选的。
我建议改用 aws configure --profile your_profile_name
,这使您可以灵活地使用多组凭据并通过设置 AWS_Profile
环境变量来控制使用哪一组。您可以在项目属性中执行此操作,例如使用带有自定义启动配置文件的 launchsettings.json
文件:
{
"profiles": {
"LaunchProfileName": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"AWS_PROFILE": "your_profile_name"
}
}
}
}
还有一些其他的环境变量也可以使用,请参阅本文末尾的引用以了解更多选项。post。
当应用程序实际部署到 AWS 时,它会自动使用 execution role 为 lambda 函数设置(或其他类型服务的类似机制,请参阅本文末尾的列表 post).
⚠ 请注意,Windows 的 %USERPROFILE%\.aws\credentials
和 Linux/MacOs 的 ~/.aws/credentials
将以未加密方式保存共享凭据文件。 ⚠
其他提供凭据的选项请参考documentation. Specifically the page about Credential and profile resolution很有帮助:
Credential search order
Credentials that are explicitly set on the AWS service client, as
described in Accessing credentials and profiles in an application.
A credentials profile with the name specified by a value in
AWSConfigs.AWSProfileName.
A credentials profile with the name specified by the AWS_PROFILE
environment variable.
The [default] credentials profile.
SessionAWSCredentials that are created from the AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN environment variables, if
they're all non-empty.
BasicAWSCredentials that are created from the AWS_ACCESS_KEY_ID and
AWS_SECRET_ACCESS_KEY environment variables, if they're both
non-empty.
IAM Roles for Tasks for Amazon ECS tasks.
Amazon EC2 instance metadata.
我目前正在维护一个由另一个团队编写的项目,有些地方我一直无法理解。
他们集成了 测试 连接到 AWS 资源,如 Dynamo、S3 等。在他们的代码中,他们使用无参数构造函数,显然测试使用默认凭据运行。
例如:
new AmazonDynamoDBClient();
如何实现与默认构造函数的连接?有人可以帮我吗?
注意:暂时不能改成依赖注入模型
对于本地开发,凭据是从 shared config and credentials file which on your local machine can be set up by using the aws configure
cli command with an AWS Access Key. The aws configure
command will ask for your access key ID, Secret token and a default region code 加载的。这 3 个是无参数构造函数工作所必需的,第四个参数 Default output format
是可选的。
我建议改用 aws configure --profile your_profile_name
,这使您可以灵活地使用多组凭据并通过设置 AWS_Profile
环境变量来控制使用哪一组。您可以在项目属性中执行此操作,例如使用带有自定义启动配置文件的 launchsettings.json
文件:
{
"profiles": {
"LaunchProfileName": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"AWS_PROFILE": "your_profile_name"
}
}
}
}
还有一些其他的环境变量也可以使用,请参阅本文末尾的引用以了解更多选项。post。
当应用程序实际部署到 AWS 时,它会自动使用 execution role 为 lambda 函数设置(或其他类型服务的类似机制,请参阅本文末尾的列表 post).
⚠ 请注意,Windows 的 %USERPROFILE%\.aws\credentials
和 Linux/MacOs 的 ~/.aws/credentials
将以未加密方式保存共享凭据文件。 ⚠
其他提供凭据的选项请参考documentation. Specifically the page about Credential and profile resolution很有帮助:
Credential search order
Credentials that are explicitly set on the AWS service client, as described in Accessing credentials and profiles in an application.
A credentials profile with the name specified by a value in AWSConfigs.AWSProfileName.
A credentials profile with the name specified by the AWS_PROFILE environment variable.
The [default] credentials profile.
SessionAWSCredentials that are created from the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN environment variables, if they're all non-empty.
BasicAWSCredentials that are created from the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables, if they're both non-empty.
IAM Roles for Tasks for Amazon ECS tasks.
Amazon EC2 instance metadata.