Created
December 20, 2017 15:08
-
-
Save ripiuk/ae0ec9bfe4f36b846c151fbda18d15cd to your computer and use it in GitHub Desktop.
Upload data to s3 in tornado asynchronously
This file contains hidden or 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
import boto3 | |
from tornado.gen import coroutine, Task | |
from tornado.httpclient import HTTPError, AsyncHTTPClient, HTTPRequest | |
_http_client = AsyncHTTPClient() | |
@coroutine | |
def upload_to_s3(bucket: str, region: str, data, key: str, | |
aws_access_key_id: str, aws_secret_access_key: str) -> str: | |
print("Start uploading to s3. Path: %s", key) | |
s3 = boto3.client('s3', region, aws_access_key_id=aws_access_key_id, | |
aws_secret_access_key=aws_secret_access_key) | |
url = s3.generate_presigned_url( | |
'put_object', | |
Params={ | |
'Bucket': bucket, | |
'Key': key | |
} | |
) | |
request = HTTPRequest( | |
url, | |
method='PUT', | |
body=data | |
) | |
response = yield Task(_http_client.fetch, request) | |
if response.error or response.code != 200: | |
print("Response that was received %s", response) | |
raise HTTPError(code=response.code, message=response.error, response=response) | |
return url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment