AWS Elasticsearch python实现

时间:2017-03-27 14:26:37

标签: python amazon-web-services elasticsearch boto amazon-elasticsearch

我正在使用python boto编写AWS Elasticsearch快照备份/恢复实现。我能够将AWS S3注册为ES快照存储库。这是我的创建快照功能的片段。

import boto 
import urllib
from boto.connection import AWSAuthConnection

class ESConnection(AWSAuthConnection):

def __init__(self, region, **kwargs):
  super(ESConnection, self).__init__(**kwargs)
  self._set_auth_region_name(region)
  self._set_auth_service_name("es")

def _required_auth_capability(self):
  return ['hmac-v4']
def create_snapshots(profile, region_name, es_host,total_snapshots_to_keep):
.
.
.
url = "/_snapshot/es_repository/{}?".format(snapshot_name)
flag = urllib.urlencode({'wait_for_completion':'true'})
resp = client.make_request(method='PUT',
    path=urllib.quote("{}{}".format(url,flag)),
    data='{"indices": "' + audit_index_name + '", "ignore_unavailable": true, "include_global_state": false}')

主要问题似乎在上面的片段中,我正在尝试为

构建一个url
  

{u'status':400,u'error':{u'root_cause':[{u'reason':   U'[es_repository:v4_2017_03_27_snapshot wait_for_completion =真]   快照名称无效   [v4_2017_03_27_snapshot?wait_for_completion = true],不得包含   以下字符[\,/,*,?,“,<,>,| ,,,,'',u'type':   u'invalid_snapshot_name_exception'}],你''':   u'invalid_snapshot_name_exception',u'reason':   U'[es_repository:v4_2017_03_27_snapshot wait_for_completion =真]   快照名称无效   [v4_2017_03_27_snapshot?wait_for_completion = true],不得包含   以下字符[\,/,*,?,“,<,>,| ,,,,'}}}

它将我的实际snapshot_name和wait_for_completion标志完全作为快照名称

  

无效的快照名称[v4_2017_03_27_snapshot?wait_for_completion = true],不得包含   以下字符[\,/,*,?,“,<,>,| ,,,,'}}}

你能否帮我指出我在构建弹性搜索网址时错误地做错的地方?或者有更好的方法来实现这一目标吗?

1 个答案:

答案 0 :(得分:0)

The error message already give you the answer. There is some special symbol not supported by s3 object name.

# the ? ending is wrong, perhaps residual of typo from other language? 
url = "/_snapshot/es_repository/{}?".format(snapshot_name)

# correct path 
url = "/_snapshot/es_repository/{}".format(snapshot_name)

(update)

When you use curl , you are using the RESTful API. The "?" mark is notify RESTful API that parameter is follow behind.

However, in your code, you use client.make_request, which is part of boto API. the make_request module only accept typical S3 "path" AKA valid S3 object name.

If you want to simulate the same RESTful path, then I suggest you use python requests with path you just construct. i.e.

import requests
path = urllib.quote("{}{}".format(url,flag))
endpoint_url = "http://.."  # find your snapshot S3 endpoint url and put here   
url = "{}/{}".format(endpoint_url, path) 
data='{"indices": "' + audit_index_name + '", "ignore_unavailable": true, "include_global_state": false}'
rest_resp= requests.post(url, data = data)

Due to boto/boto2 confusing documentation and deprecate issues, you should use boto3