from StringIO import StringIO
from fabric.api import get
fd = StringIO()
get(remote_path, fd)
content=fd.getvalue()
How to get the content of a remote file without a local temporary file with fabric
Question
I want to get the content of a remote file with fabric, without creating a temporary file.
Solution
OTHER TIPS
With Python 3 (and fabric3), I get this fatal error when using io.StringIO
: string argument expected, got 'bytes'
, apparently because Paramiko writes to the file-like object with bytes. So I switched to using io.BytesIO
and it works:
from io import BytesIO
def _read_file(file_path, encoding='utf-8'):
io_obj = BytesIO()
get(file_path, io_obj)
return io_obj.getvalue().decode(encoding)
import tempfile
from fabric.api import get
with tempfile.TemporaryFile() as fd:
get(remote_path, fd)
fd.seek(0)
content=fd.read()
See: http://docs.python.org/2/library/tempfile.html#tempfile.TemporaryFile
and: http://docs.fabfile.org/en/latest/api/core/operations.html#fabric.operations.get
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow