Created
February 27, 2017 14:59
-
-
Save vangheem/bc53f289066dad073ec554b7819c492c to your computer and use it in GitHub Desktop.
Streaming s3 file for downloads
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
# using aiobotocore and plone.server(aiohttp) | |
ob = await client.get_object(Bucket=bucket, Key=key) | |
info = ob['ResponseMetadata']['HTTPHeaders'] | |
resp = aiohttp.web.StreamResponse(headers=aiohttp.MultiDict({ | |
'CONTENT-DISPOSITION': 'attachment; filename="%s"' % self.filename, | |
'Content-Type': info['content-type'] | |
})) | |
resp.content_type = info['content-type'] | |
resp.content_length = info['content-length'] | |
await resp.prepare(self.request) | |
resp.start(self.request) | |
async with ob['Body'] as stream: | |
file_data = await stream.read(self.chunk_size) | |
while file_data: | |
resp.write(file_data) | |
file_data = await stream.read(self.chunk_size) | |
return resp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment