ElasticSearch.NET在POST请求中传递AWS Signature v4?

时间:2018-11-27 00:33:05

标签: c# .net amazon-web-services elasticsearch

我对ElasticSearch还是很陌生,并且已经在c#中设置了一个AWS Lambda函数来获取S3对象(包含JSON数据)的内容,希望将它们发布到ES上以便于搜索。

我正在使用Elasticsearch.Net nuget库。

这里的文档-https://github.com/elastic/elasticsearch-net中有配置节点URI等的示例,但是我的理解是,对ES的任何请求都需要使用AWS Signature V4(基于Access / Secret密钥)进行签名。我已经为此创建了一个IAM用户,但是在文档中找不到任何地方可以签名该POST请求。示例显示了张贴方法,但是没有地方包含签名吗?

例如

var person = new Person
{
    FirstName = "Martijn",
    LastName = "Laarman"
};

var indexResponse = lowlevelClient.Index<BytesResponse>("people", "person", "1", PostData.Serializable(person)); 
byte[] responseBytes = indexResponse.Body;

var asyncIndexResponse = await lowlevelClient.IndexAsync<StringResponse>("people", "person", "1", PostData.Serializable(person)); 
string responseString = asyncIndexResponse.Body;

即使实例化连接,也没有地方可以添加您的凭据?

var settings = new ConnectionConfiguration(new Uri("http://example.com:9200"))
    .RequestTimeout(TimeSpan.FromMinutes(2));

var lowlevelClient = new ElasticLowLevelClient(settings);

我已经检查了ConnectionConfiguration对象,但是似乎没有相关的方法或属性。我错过了什么?

2 个答案:

答案 0 :(得分:0)

// if using an access key
var httpConnection = new AwsHttpConnection("us-east-1", new StaticCredentialsProvider(new AwsCredentials
{
    AccessKey = "My AWS access key",
    SecretKey = "My AWS secret key",
}));

// if using app.config, environment variables, or roles
var httpConnection = new AwsHttpConnection("us-east-1");

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var config = new ConnectionSettings(pool, httpConnection);
var client = new ElasticClient(config);

答案 1 :(得分:0)

使用elasticsearch-net-aws软件包,您应该能够像这样设置低级客户端:


var httpConnection = new AwsHttpConnection("us-east-1"); // or whatever region you're using
var pool = new SingleNodeConnectionPool(new Uri(TestConfig.Endpoint));
var config = new ConnectionConfiguration(pool, httpConnection);
config.DisableDirectStreaming();
var client = new ElasticLowLevelClient(config);
// ...