从S3流式传输大文件至HTTP Multipart端点失败求助
问题:S3大文件流式传输到HTTP API失败
尝试将S3存储桶中的大文件流式传输到HTTP API,查阅了诸多Stack Overflow帖子但未能整合出可行方案。本地文件使用requests-toolbelt的MultipartEncoder可正常上传,代码如下:
stream = open('/tmp/file', 'rb') encoder = MultipartEncoder( {'attachments': ('file', stream, 'application/octet-stream'), 'canRename': 'true'} ) headers['Content-Type'] = encoder.content_type r = requests.post(f'/api/attachments', data=encoder, headers=headers) print(r.text)
但使用S3对象的Body流时失败,代码如下:
obj = s3_client.get_object(Bucket='bucket', Key='key') stream = obj['Body'] encoder = MultipartEncoder( {'attachments': ('file', stream, 'application/octet-stream'), 'canRename': 'true'} ) headers['Content-Type'] = encoder.content_type r = requests.post(f'/api/attachments', data=encoder, headers=headers) print(r.text)
报错信息:
self = <requests_toolbelt.multipart.encoder.FileWrapper object at 0x1004d4f10>
return total_len(self.fd) - self.fd.tell()
E TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
尝试用不同方式包装流但均无效:
stream = StreamingIterator(obj['ContentLength'], obj['Body']) # 传输数据但永久挂起 stream = io.BufferedReader(obj['Body']._raw_stream, obj['ContentLength']) # 导致编码器无法计算总长度
内容的提问来源于stack exchange,提问作者Plup
相关产品推荐
相关产品推荐

