访问应用程序中的凭证和配置文件 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

访问应用程序中的凭证和配置文件

使用凭证的首选方法是允许Amazon SDK for .NET为您查找和检索凭证,如凭证和配置文件解析中所述。

但是,您还可以将应用程序配置为主动检索配置文件和凭证,然后在创建 Amazon 服务客户端时明确使用这些凭证。

要主动检索配置文件和凭证,请使用 Amazon.Runtime.CredentialManagement 命名空间中的类。

  • 要在使用 Amazon 凭证文件格式(默认位置的共享 Amazon 凭证文件或自定义凭证文件)的文件中查找配置文件,请使用 SharedCredentialsFile 类。为简洁起见,这种格式的文件有时在此文本中简称为凭证文件

  • 要在 SDK Store 中查找配置文件,请使用 NetSDKCredentialsFile 类。

  • 要同时在凭证文件和 SDK Store 中进行搜索,根据类属性的配置,请使用 CredentialProfileStoreChain 类。

    您可以使用该类来查找配置文件。您也可以使用该类直接请求 Amazon 凭证,而不是使用 AWSCredentialsFactory 类(如下所述)。

  • 要从配置文件中检索或创建各种类型的凭证,请使用 AWSCredentialsFactory 类。

以下部分提供了这些类的示例。

CredentialProfileStoreChain 类的示例

可使用 TryGetAWSCredentialsTryGetProfile 方法获取 CredentialProfileStoreChain 类的凭证或配置文件。类的 ProfilesLocation 属性将决定方法的行为,如下所示:

  • 如果 ProfilesLocation 为 null 或空,则在平台支持的情况下搜索 SDK Store,然后在默认位置搜索共享 Amazon 凭证文件。

  • 如果 ProfilesLocation 属性包含值,请搜索该属性中指定的凭证文件。

从 SDK Store 或共享 Amazon 凭证文件获取凭证

本示例向您展示了如何使用 CredentialProfileStoreChain 类获取凭证,然后使用这些凭证创建 AmazonS3Client 对象。这些凭证可以来自 SDK Store,也可以来自默认位置的共享 Amazon 凭证

本示例还使用了 Amazon.Runtime.AWSCredentials 类。

var chain = new CredentialProfileStoreChain(); AWSCredentials awsCredentials; if (chain.TryGetAWSCredentials("some_profile", out awsCredentials)) { // Use awsCredentials to create an Amazon S3 service client using (var client = new AmazonS3Client(awsCredentials)) { var response = await client.ListBucketsAsync(); Console.WriteLine($"Number of buckets: {response.Buckets.Count}"); } }

从 SDK Store 或共享 Amazon 凭证文件获取配置文件

本示例说明如何使用 CredentialProfileStoreChain 类获取配置文件。这些凭证可以来自 SDK Store,也可以来自默认位置的共享 Amazon 凭证

本示例还使用了 CredentialProfile 类。

var chain = new CredentialProfileStoreChain(); CredentialProfile basicProfile; if (chain.TryGetProfile("basic_profile", out basicProfile)) { // Use basicProfile }

从自定义凭证文件获取凭证

本示例说明如何使用 CredentialProfileStoreChain 类获取凭证。凭证来自使用 Amazon 凭证文件格式但位于备用位置的文件。

本示例还使用了 Amazon.Runtime.AWSCredentials 类。

var chain = new CredentialProfileStoreChain("c:\\Users\\sdkuser\\customCredentialsFile.ini"); AWSCredentials awsCredentials; if (chain.TryGetAWSCredentials("basic_profile", out awsCredentials)) { // Use awsCredentials to create an AWS service client }

SharedCredentialsFile 和 AWSCredentialsFactory 类的示例

使用 SharedCredentialsFile 类创建 AmazonS3Client

此示例向您展示了如何在共享 Amazon 凭证文件中查找配置文件,根据该配置文件创建 Amazon 凭证,然后使用这些凭证创建 AmazonS3Client 对象。该示例使用 SharedCredentialsFile 类。

此示例还使用了 CredentialProfile 类和 Amazon.Runtime.AWSCredentials 类。

CredentialProfile basicProfile; AWSCredentials awsCredentials; var sharedFile = new SharedCredentialsFile(); if (sharedFile.TryGetProfile("basic_profile", out basicProfile) && AWSCredentialsFactory.TryGetAWSCredentials(basicProfile, sharedFile, out awsCredentials)) { // use awsCredentials to create an Amazon S3 service client using (var client = new AmazonS3Client(awsCredentials, basicProfile.Region)) { var response = await client.ListBucketsAsync(); Console.WriteLine($"Number of buckets: {response.Buckets.Count}"); } }
注意

NetSDKCredentialsFile 类的使用方式完全相同,唯一的不同是您要实例化一个新的 NetSDKCredentialsFile 对象,而不是 SharedCredentialsFile 对象。