question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to Use botocore.response.StreamingBody as stdin PIPE

See original GitHub issue

I want to pipe large video files from AWS S3 into Popen’s stdin. This code runs as an AWS Lambda function, so these files won’t fit in memory or on the local file system. Also, I don’t want to copy these huge files anywhere, I just want to stream the input, process on the fly, and stream the output. I’ve already got the processing and streaming output bits working. The problem is how to obtain an input stream as a Popen pipe.

I can access a file in an S3 bucket:

import boto3
s3 = boto3.resource('s3')
response = s3.Object(bucket_name=bucket, key=key).get()
body = response['Body']  

body is a botocore.response.StreamingBody. I intend to use body something like this:

from subprocess import Popen, PIPE
Popen(cmd, stdin=PIPE, stdout=PIPE).communicate(input=body)[0]

But of course body needs to be converted into a file-like object. The question is how?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:38 (3 by maintainers)

github_iconTop GitHub Comments

33reactions
royrussocommented, Feb 16, 2016

Not sure if this helps, but I struggled with a similar issue streaming from boto3:s3 to flask output stream. Some sample code here, may help you:

s3_response = s3_client.get_object(Bucket=BUCKET, Key=FILENAME)

def generate(result):
   for chunk in iter(lambda: result['Body'].read(self.CHUNK_SIZE), b''):
      yield chunk

return Response(generate(s3_response), mimetype='application/zip', headers={'Content-Disposition': 'attachment;filename=' + FILENAME})
`
18reactions
mslinncommented, Jan 8, 2016

I wish it were so simple. I must have dumped at least 30 hours into this problem, and I’ve tried a variety of approaches. I’ve tried the AWS & Python communities on StackOverflow but got no response. I think this issue requires too much setup for a generic Python programmer to address.

I’m sure this will be an often-repeated question, I’m just the first to hit it. I think someone from AWS should take it up.

For my part, I’ve got a broken product in production as a result of this issue. I am willing to pay cash money for a fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use botocore.response.StreamingBody as stdin PIPE
1 Answer 1 · 5. Calling read() loads the entire video (hundreds of MB) into RAM. · 1. @MikeSlinn StreamingBody. · 1. I...
Read more >
Developers - How to Use botocore.response.StreamingBody as ...
How to Use botocore.response.StreamingBody as stdin PIPE.
Read more >
Response Reference — botocore 1.29.34 documentation - AWS
class botocore.response. StreamingBody (raw_stream ... Wrapper class for an http response body. ... Close the underlying http response stream.
Read more >
boto3 streamingBody to BytesIO - Serious Autonomous Vehicles
the following discussion is really really helpful: boto3 issue #426: how to use botocore.response.StreamingBody as stdin PIPE.
Read more >
Process Files through Unix pipeline in AWS Lambda - Medium
First, we obtain the input data stream using boto3 : import boto3 ... stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=bufsize).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found