根据 Norm 的回答,我找到了解释如何使用共享凭证的资源:https ://medium.com/@somchat/programming-using-aws-net-sdk-9ce3f5119633
这就是我之前使用 NetSDKCredentials 的方式,它不适用于 Linux/Mac OS:
//Try this code on a non-Windows platform and you will see the above error
var options = new CredentialProfileOptions
{
AccessKey = "access_key",
SecretKey = "secret_key"
};
var profile = new CredentialProfile("default", options);
profile.Region = RegionEndpoint.USWest1;
NetSDKCredentialsFile file = new NetSDKCredentialsFile();
file.RegisterProfile(profile);
但是我随后能够使用此示例来使用 SharedCredentials:
var credProfileStoreChain = new CredentialProfileStoreChain();
if (credProfileStoreChain.TryGetAWSCredentials("default", out AWSCredentials awsCredentials))
{
Console.WriteLine("Access Key: " + awsCredentials.GetCredentials().AccessKey);
Console.WriteLine("Secret Key: " + awsCredentials.GetCredentials().SecretKey);
}
Console.WriteLine("Hello World!");
然后,您将能够看到您的代码能够访问密钥:
Access Key: A..................Q
Secret Key: 8.......................................p
Hello World!
然后我使用 System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform() (因为我在 Windows 和 Linux 上都使用此代码)来确定要使用的凭据:
using System.Runtime.InteropServices;
//NETSDK Credentials only work on Windows - must use SharedCredentials on Linux
bool isLinux = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
if (isLinux) {
//Use SharedCredentials
} else {
//Use NetSDKCredentials
}
您可能会发现 AWS 文档的这一部分也很有帮助:https ://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html#creds-locate