我正在尝试测试 .NET Core 控制台程序以将消息发布到 SNS。由于我在尝试让它在 Lambda 中工作时遇到问题,因此我想在非 Lambda 环境中尝试一下。在 Lambda 中,安全性由角色负责,但在控制台程序中,我认为我必须以某种方式指定我的访问密钥和秘密。
我读过这一页:http://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html#net-dg-config-creds-sdk-store http://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html#net-dg-config-creds-sdk-store,但仍然完全困惑。
我在本地开发计算机上运行,而不是 EC2 实例。无意将其投入生产,只是尝试测试一些代码。
我使用的是 Visual Studio 2015,.NET Core 1.0。我使用 Nuget 获得以下内容:
"AWSSDK.Extensions.NETCore.Setup": "3.3.3",
"AWSSDK.SimpleNotificationService": "3.3.0.23",
根据以下问题的答案如何在 NET Core 上的 AWS SDK 上设置凭证? https://stackoverflow.com/questions/43053495/how-to-set-credentials-on-aws-sdk-on-net-core我创建了 /user/.aws/credentials 文件(假设凭证是文件名而不是目录名)。
但该问题/答案并未解决如何实际使用该文件。我正在运行的代码如下。
public static void Main(string[] args)
{
Console.WriteLine("Started");
//var awsCredentials = new Amazon.Runtime.AWSCredentials()
var client = new Amazon.SimpleNotificationService.AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.EUWest2);
//var client = new Amazon.SimpleNotificationService.AmazonSimpleNotificationServiceClient(awsCredentials, Amazon.RegionEndpoint.EUWest2);
//Amazon.SimpleNotificationService.Model.PublishResponse publishResp = null;
SendMessage(client).Wait();
Console.WriteLine("Completed call to SendMessage: Press enter to end:");
Console.ReadLine();
}
我在新客户端上遇到的错误是:
An unhandled exception of type 'Amazon.Runtime.AmazonServiceException' occurred in AWSSDK.Core.dll
Additional information: Unable to find credentials
我看到有一种方法可以将 AWSCredentials 对象传递给该构造函数,但我不明白如何构建它。 Amazon.Runtime.AWSCredentials 是一个抽象类,因此我无法在“new”语句中使用它。