我希望使用 Amazon 的 Elasticsearch 服务器来支持 Django 数据库中长文本字段的搜索。然而,我也不想将此搜索暴露给那些没有登录并且不想通过模糊或某些 IP 限制策略来依赖安全性的人(除非它可以很好地与现有的 heroku 应用程序配合使用,部署 Django 应用程序的位置)。
Haystack 似乎在这方面走了很长的路,但似乎没有一种简单的方法将其配置为使用 Amazon 的 IAM 凭证来访问 Elasticsearch 服务。这个功能确实存在于它使用的elasticsearch-py中。
https://elasticsearch-py.readthedocs.org/en/master/#running-with-aws-elasticsearch-service https://elasticsearch-py.readthedocs.org/en/master/#running-with-aws-elasticsearch-service
from elasticsearch import Elasticsearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
host = 'YOURHOST.us-east-1.es.amazonaws.com'
awsauth = AWS4Auth(YOUR_ACCESS_KEY, YOUR_SECRET_KEY, REGION, 'es')
es = Elasticsearch(
hosts=[{'host': host, 'port': 443}],
http_auth=awsauth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection
)
print(es.info())
关于使用 HTTP 授权,我在以下问题下发现了这一点:https://github.com/django-haystack/django-haystack/issues/1046 https://github.com/django-haystack/django-haystack/issues/1046
from urlparse import urlparse
parsed = urlparse('https://user:pass@host:port')
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': parsed.hostname,
'INDEX_NAME': 'haystack',
'KWARGS': {
'port': parsed.port,
'http_auth': (parsed.username, parsed.password),
'use_ssl': True,
}
}
}
我想知道是否有一种方法可以将这两者结合起来,如下所示(正如预期的那样,它会给出错误,因为它不仅仅是用户名和密码):
from requests_aws4auth import AWS4Auth
awsauth = AWS4Auth([AACCESS_KEY],[SECRET_KEY],[REGION],'es')
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': [AWSHOST],
'INDEX_NAME': 'haystack',
'KWARGS': {
'port': 443,
'http_auth': awsauth,
'use_ssl': True,
'verify_certs': True
}
},
}
这里的错误:
TypeError at /admin/
must be convertible to a buffer, not AWS4Auth
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.7.7
Exception Type: TypeError
Exception Value:
must be convertible to a buffer, not AWS4Auth
Exception Location: /usr/lib/python2.7/base64.py in b64encode, line 53
关于如何实现这一目标有什么想法吗?