Skip to content

Instantly share code, notes, and snippets.

@akarve
Created January 30, 2019 23:49
Show Gist options
  • Save akarve/a43f86a10e8d22ffdbfbb16a89500d2b to your computer and use it in GitHub Desktop.
Save akarve/a43f86a10e8d22ffdbfbb16a89500d2b to your computer and use it in GitHub Desktop.
def post_to_es(event_type, size, text, key, meta, version_id=''):
ES_HOST = os.environ['ES_HOST']
ES_INDEX = 'drive'
data = {
'type': event_type,
'size': size,
'text': text,
'key': key,
'updated': datetime.utcnow().isoformat(),
'version_id': version_id
}
data = {**data, **transform_meta(meta)}
data['meta_text'] = ' '.join([data['meta_text'], key])
try:
session = boto3.session.Session()
awsauth = AWSRequestsAuth(
aws_access_key=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
aws_token=os.environ['AWS_SESSION_TOKEN'],
aws_host=ES_HOST,
aws_region=session.region_name,
aws_service='es'
)
es = Elasticsearch(
hosts=[{'host': ES_HOST, 'port': 443}],
http_auth=awsauth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection
)
res = es.index(index=ES_INDEX, doc_type='_doc', body=data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment