使用 .Net SDK 获取当前的 AWS 配置文件
在开发计算机上执行的 C# 项目中,我想要获取当前 AWS 配置文件的 AWS 区域。我已经看到 凭证和个人资料解决文档,如果我可以避免它,我宁愿不重新实现它。在 bash 中我会使用:
Region=$(aws configure get region)
AWS .Net SDK 中是否有一些调用可以执行相同的操作?
In a C# project being executed on the development machine, I want to get the AWS region for my current AWS profile. I've seen the credential and profile resolution documentation and would rather not reimplement this if I can avoid it. In bash I would use:
Region=$(aws configure get region)
Is there some call in the AWS .Net SDK to do the equivalent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为某些服务创建 AWS 客户端对象后,您可以通过客户端对象的 Config 属性访问默认用户的区域。
此两行示例使用默认凭证创建一个客户端,然后在控制台上显示 RegionEndpoint 值:
此代码使用适用于 .NET 版本 3.7 和 .NET Core 5 的 AWS 开发工具包。以下是 IClientConfig 的 API 文档的链接界面,以便您可以查看其他可用信息:IClientConfig
Once you have created an AWS client object for some service, you can access the region for the default user through the Config property of the client object.
This two-line example creates a client using the default credentials and then displays the RegionEndpoint value on the console:
This code uses the AWS SDK for .NET version 3.7 and .NET Core 5. Here is a link to the API documentation for the IClientConfig interface so you can see what other information is available: IClientConfig
这个问题已经很老了,但如果有人正在寻找稍微不同的答案(就像我一样:-),我想分享。
另一个可能的目标是从任何服务客户端上下文之外的命名配置文件读取区域。
AWS CLI 中的等效项可能是:
例如,您可能想要执行此操作,以便在创建服务客户端时可以使用区域设置。
以下代码(或类似代码)将实现此目标:
注意:请注意,此过程提供了为指定配置文件显式配置的区域。它不考虑任何类型的后备区域搜索。
希望这有帮助。
This question is pretty old, but in case anyone was looking for a slightly different answer (like I was :-), I wanted to share.
Another possible goal is to read the Region from a named profile outside the context of any service client.
The equivalent in the AWS CLI might be:
You might want to do this, for example, so that you can use the Region setting while creating a service client.
The following code (or similar) will accomplish this goal:
Note: Be aware that this procedure gives the region that is explicitly configured for the named profile. It doesn't take into account any sort of fallback region search.
Hope this is helpful.