Created
May 1, 2017 12:03
-
-
Save nanvel/361c9ac3f795d9c4d8d2d0680eb66b31 to your computer and use it in GitHub Desktop.
Twisted AWS client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Lock | |
from botocore.endpoint import Endpoint | |
import botocore.session | |
import treq | |
__all__ = ('AWSClient',) | |
class BotocoreRequest(Exception): | |
def __init__(self, request, *args, **kwargs): | |
super(BotocoreRequest, self).__init__(*args, **kwargs) | |
self.method = request.method | |
# https://github.com/twisted/treq/issues/185 | |
self.url = request.url.replace('https://', 'http://') | |
self.headers = dict(request.headers) | |
def _send_request(self, request_dict, operation_model): | |
request = self.create_request(request_dict, operation_model) | |
raise BotocoreRequest(request=request) | |
class AWSClient(object): | |
""" | |
client = AWSClient( | |
service='s3', | |
access_key=AWS_ACCESS_KEY, | |
secret_key=AWS_SECRET_KEY, | |
region=AWS_S3_REGION | |
) | |
df = client.request( | |
method='put_object', | |
ACL='private', | |
Bucket=AWS_S3_BUCKET, | |
Key='hi/hi4.json', | |
Body="{}" | |
) | |
df = client.request( | |
method='get_object', | |
Bucket=AWS_S3_BUCKET, | |
Key='hi/hi3.json' | |
) | |
""" | |
def __init__(self, service, access_key, secret_key, region, timeout=30): | |
session = botocore.session.get_session() | |
session.set_credentials( | |
access_key=access_key, | |
secret_key=secret_key | |
) | |
self.client = session.create_client(service, region_name=region) | |
self.timeout = timeout | |
def request(self, method, **kwargs): | |
_send_request_original = Endpoint._send_request | |
lock = Lock() | |
try: | |
lock.acquire() | |
Endpoint._send_request = _send_request | |
getattr(self.client, method)(**kwargs) | |
except BotocoreRequest as e: | |
return treq.request( | |
method=e.method, | |
url=e.url, | |
data=kwargs.get('Body', None), | |
headers=e.headers, | |
timeout=self.timeout | |
) | |
finally: | |
Endpoint._send_request = _send_request_original | |
lock.release() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment