如何为 .NET Core 桌面应用程序创建 AWS 服务客户端对象

How to create an AWS service client object for a .NET Core desktop app

我正在将 .NET 5.0 中的应用程序编码到桌面上 运行。它将查询 AWS CloudWatch 并下载日志条目。

我正在使用以下方法创建包含查询方法的 AWS 服务客户端。然而,当程序进入CreateServiceClient()方法时,它挂起一分钟,然后returns直接转到调用我的GetLogs()方法的方法,没有错误。

using Amazon.CloudWatchLogs;
using Amazon.CloudWatchLogs.Model;
using Amazon.Extensions.NETCore.Setup;
.
.
.
public async Task<GetQueryResultsResponse> GetLogs()
{ 
       AWSOptions awsOptions = new AWSOptions { Profile = "myprofile" };
       IAmazonCloudWatchLogs logs = awsOptions.CreateServiceClient<IAmazonCloudWatchLogs>();
       .
       .
       .

我有一个位于 C:\Users\Username\.aws\credentials 的凭据文件,其内容为

[myprofile]
aws_access_key_id = <myid>
aws_secret_access_key = <mykey>

我已验证凭据可用于 AWS CLI。我错过了什么?

您没有使用最新的 .NET API。下面的示例展示了如何使用 AmazonCloudWatchLogsClient 创建 Amazon CloudWatch 日志组。该示例是使用适用于 .NET 版本 3.7 和 .NET Core 5.0 的 AWS 开发工具包创建的。

您应该使用 version 3.0 of the AWS SDK for .NET

此处有更多示例:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv3/CloudWatchLogs

namespace CreateLogGroupExample
{
    using System;
    using System.Threading.Tasks;
    using Amazon.CloudWatchLogs;
    using Amazon.CloudWatchLogs.Model;

    /// <summary>
    /// Shows how to create an Amazon CloudWatch Logs log group. The example
    /// was created using the AWS SDK for .NET version 3.7 and .NET Core 5.0.
    /// </summary>
    public class CreateLogGroup
    {
        // snippet-start:[CloudWatchLogs.dotnetv3.CreateLogGroupExample]
        public static async Task Main()
        {
            // This client object will be associated with the same AWS Region
            // as the default user on this system. If you need to use a
            // different AWS Region, pass it as a parameter to the client
            // constructor.
            var client = new AmazonCloudWatchLogsClient();

            string logGroupName = "cloudwatchlogs-example-loggroup";

            var request = new CreateLogGroupRequest
            {
                LogGroupName = logGroupName,
            };

            var response = await client.CreateLogGroupAsync(request);

            if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine($"Successfully create log group with ID: {logGroupName}.");
            }
            else
            {
                Console.WriteLine("Could not create log group.");
            }
        }

        // snippet-end:[CloudWatchLogs.dotnetv3.CreateLogGroupExample]
    }
}