使用 .Net SDK 获取当前的 AWS 配置文件

发布于 2025-01-16 10:15:29 字数 354 浏览 2 评论 0原文

在开发计算机上执行的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2025-01-23 10:15:29

为某些服务创建 AWS 客户端对象后,您可以通过客户端对象的 Config 属性访问默认用户的区域。

此两行示例使用默认凭证创建一个客户端,然后在控制台上显示 RegionEndpoint 值:

var client = new AmazonS3Client();
Console.WriteLine(client.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:

var client = new AmazonS3Client();
Console.WriteLine(client.Config.RegionEndpoint);

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

云之铃。 2025-01-23 10:15:29

这个问题已经很老了,但如果有人正在寻找稍微不同的答案(就像我一样:-),我想分享。

另一个可能的目标是从任何服务客户端上下文之外的命名配置文件读取区域。
AWS CLI 中的等效项可能是:

aws configure get region --profile my-named-profile

例如,您可能想要执行此操作,以便在创建服务客户端时可以使用区域设置。

以下代码(或类似代码)将实现此目标:

using Amazon.Runtime.CredentialManagement;

static RegionEndpoint ReadRegionEnpoint(string profileName)
{
    var chain = new CredentialProfileStoreChain();
    if(!chain.TryGetProfile(profileName, out var profile))
        throw new Exception($"Failed to find the {profileName} profile");
    return profile.Region;
}

注意:请注意,此过程提供了为指定配置文件显式配置的区域。它不考虑任何类型的后备区域搜索。

希望这有帮助。

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:

aws configure get region --profile my-named-profile

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:

using Amazon.Runtime.CredentialManagement;

static RegionEndpoint ReadRegionEnpoint(string profileName)
{
    var chain = new CredentialProfileStoreChain();
    if(!chain.TryGetProfile(profileName, out var profile))
        throw new Exception(
quot;Failed to find the {profileName} profile");
    return profile.Region;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文