Created
July 5, 2024 16:15
-
-
Save 2tony2/945bc3b84789002d340a5cf7445cc568 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pythonCopy code | |
import paramiko | |
def stream_data_from_sftp(hostname, port, username, password, remote_path, chunk_size=1024): | |
transport = paramiko.Transport((hostname, port)) | |
transport.connect(username=username, password=password) | |
sftp = paramiko.SFTPClient.from_transport(transport) | |
with sftp.open(remote_path, 'rb') as file: | |
while chunk := file.read(chunk_size): | |
yield chunk | |
sftp.close() | |
transport.close() | |
# Example usage | |
hostname = 'sftp.example.com' | |
port = 22 | |
username = 'user' | |
password = 'pass' | |
remote_path = '/path/to/large_file' | |
for chunk in stream_data_from_sftp(hostname, port, username, password, remote_path): | |
# Process each chunk | |
print(chunk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment