How to get the content of a remote file without a local temporary file with fabric

StackOverflow https://stackoverflow.com/questions/19319014

  •  30-06-2022
  •  | 
  •  

Question

I want to get the content of a remote file with fabric, without creating a temporary file.

Was it helpful?

Solution

from StringIO import StringIO
from fabric.api import get

fd = StringIO()
get(remote_path, fd)
content=fd.getvalue()

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
scroll top