Skip to content

Instantly share code, notes, and snippets.

@vangheem
Created February 27, 2017 14:59
Show Gist options
  • Save vangheem/bc53f289066dad073ec554b7819c492c to your computer and use it in GitHub Desktop.
Save vangheem/bc53f289066dad073ec554b7819c492c to your computer and use it in GitHub Desktop.
Streaming s3 file for downloads
# 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