2016-06-28 00:11:46 +02:00
|
|
|
"""
|
|
|
|
.. module: lemur.plugins.lemur_aws.s3
|
|
|
|
:platform: Unix
|
|
|
|
:synopsis: Contains helper functions for interactive with AWS S3 Apis.
|
2018-05-29 19:18:16 +02:00
|
|
|
:copyright: (c) 2018 by Netflix Inc., see AUTHORS for more
|
2016-06-28 00:11:46 +02:00
|
|
|
:license: Apache, see LICENSE for more details.
|
|
|
|
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
|
|
|
|
"""
|
2017-05-20 20:09:55 +02:00
|
|
|
from flask import current_app
|
|
|
|
from .sts import sts_client
|
2016-06-28 00:11:46 +02:00
|
|
|
|
2017-05-20 20:09:55 +02:00
|
|
|
|
2017-06-28 01:18:34 +02:00
|
|
|
@sts_client('s3', service_type='resource')
|
|
|
|
def put(bucket_name, region, prefix, data, encrypt, **kwargs):
|
2016-06-28 00:11:46 +02:00
|
|
|
"""
|
|
|
|
Use STS to write to an S3 bucket
|
|
|
|
"""
|
2017-06-28 01:18:34 +02:00
|
|
|
bucket = kwargs['resource'].Bucket(bucket_name)
|
2017-05-20 20:09:55 +02:00
|
|
|
current_app.logger.debug('Persisting data to S3. Bucket: {0} Prefix: {1}'.format(bucket_name, prefix))
|
2016-06-28 00:11:46 +02:00
|
|
|
|
2017-06-28 01:18:34 +02:00
|
|
|
# get data ready for writing
|
|
|
|
if isinstance(data, str):
|
|
|
|
data = data.encode('utf-8')
|
|
|
|
|
2017-05-20 20:09:55 +02:00
|
|
|
if encrypt:
|
|
|
|
bucket.put_object(
|
|
|
|
Key=prefix,
|
2017-06-28 01:18:34 +02:00
|
|
|
Body=data,
|
2017-05-20 20:09:55 +02:00
|
|
|
ACL='bucket-owner-full-control',
|
|
|
|
ServerSideEncryption='AES256'
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
bucket.put_object(
|
|
|
|
Key=prefix,
|
2017-06-28 01:18:34 +02:00
|
|
|
Body=data,
|
2017-05-20 20:09:55 +02:00
|
|
|
ACL='bucket-owner-full-control'
|
|
|
|
)
|