【问题标题】:AWS Elasticsearch python implementationAWS Elasticsearch python 实施
【发布时间】:2017-08-20 07:42:05
【问题描述】:

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

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}')

主要问题似乎在 sn-p 上面,我正在尝试为

构造一个 url

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

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

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

您能否帮我指出我在为 elasticsearch 构建 url 时做错的地方?或者有没有更好的方法来实现这一点?

【问题讨论】:

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


    【解决方案1】:

    错误信息已经给你答案了。 s3 对象名称不支持某些特殊符号。

    # 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)
    

    (更新)

    当您使用 curl 时,您使用的是 RESTful API。这 ”?”标记是通知 RESTful API 参数在后面。

    但是,在您的代码中,您使用 client.make_request,它是 boto API 的一部分。 make_request 模块仅接受典型的 S3“路径”AKA 有效的 S3 对象名称。

    如果你想模拟相同的 RESTful 路径,那么我建议你使用 python requests 和你刚刚构建的路径。即

    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)
    

    由于 boto/boto2 令人困惑的文档和弃用问题,您应该使用 boto3

    【讨论】:

    • 是的,我只需要一个答案。基本问题在于 URL 本身的限制。我试着放置'?在不同的地方标记,即在“/_snapshot/es_repository/{}?”或在快照名称中或不将其放置在任何地方。我在这个问题中展示的一个是用例之一,我在构建 url 时需要帮助。如果我像 curl -XPUT 'localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true&pretty' 那样手动执行它,如果使用脚本它会失败抱怨“?”。
    猜你喜欢
    • 2018-09-23
    • 2017-08-16
    • 1970-01-01
    • 2016-11-26
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    相关资源
    最近更新 更多