简体   繁体   English

使用 Python 访问 Amazon EC2 服务器上的文件

[英]Access files on Amazon EC2 server with Python

Apologies if this is a really simple question, but I cannot find any information on Google to help.如果这是一个非常简单的问题,我深表歉意,但我在 Google 上找不到任何信息来提供帮助。

I have an Amazon EC2 server set-up, which collects data which is streamed to the server by some scientific instruments and saves this as.csv files.我有一个 Amazon EC2 服务器设置,它收集由一些科学仪器流式传输到服务器的数据,并将其保存为.csv 文件。

I want to access these.csv files from a Python script running on my (remote) laptop.我想从在我的(远程)笔记本电脑上运行的 Python 脚本访问这些.csv 文件。 Is this possible?这可能吗?

My understanding is that I would need my Python code to 'login' to the server and then download the file to a local directory.我的理解是,我需要我的 Python 代码“登录”到服务器,然后将文件下载到本地目录。

Any help/pointers would be gratefully recieved.任何帮助/指针将不胜感激。

Update 1更新 1

I installed paramiko and tried the code below我安装了 paramiko 并尝试了下面的代码

import paramiko
paramiko.util.log_to_file("D:/Temp/aws/paramiko.log")

# Open a transport
host,port = "##.##.###.##",##
transport = paramiko.Transport((host,port))

# Auth    
username,password = "username","password"
transport.connect(None,username,password)

But, i get the following error:但是,我收到以下错误:

SSHException: Error reading SSH protocol banner

The log file looks like this:日志文件如下所示:

DEB [20210202-17:54:47.282] thr=1   paramiko.transport: starting thread (client mode): 0x5202388
DEB [20210202-17:54:47.283] thr=1   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.7.2
DEB [20210202-17:54:47.384] thr=1   paramiko.transport: Banner: 220-FileZilla Server 0.9.60 beta
DEB [20210202-17:54:47.384] thr=1   paramiko.transport: Banner: 220-written by Tim Kosse (tim.kosse@filezilla-project.org)
DEB [20210202-17:54:47.384] thr=1   paramiko.transport: Banner: 220 Please visit https://filezilla-project.org/
DEB [20210202-17:54:48.003] thr=1   paramiko.transport: Banner: 500 Syntax error, command unrecognized.
ERR [20210202-17:54:50.019] thr=1   paramiko.transport: Exception: Error reading SSH protocol banner
ERR [20210202-17:54:50.026] thr=1   paramiko.transport: Traceback (most recent call last):
ERR [20210202-17:54:50.027] thr=1   paramiko.transport:   File "C:\Users\caira\Anaconda3\lib\site-packages\paramiko\transport.py", line 2211, in _check_banner
ERR [20210202-17:54:50.027] thr=1   paramiko.transport:     buf = self.packetizer.readline(timeout)
ERR [20210202-17:54:50.027] thr=1   paramiko.transport:   File "C:\Users\caira\Anaconda3\lib\site-packages\paramiko\packet.py", line 380, in readline
ERR [20210202-17:54:50.027] thr=1   paramiko.transport:     buf += self._read_timeout(timeout)
ERR [20210202-17:54:50.027] thr=1   paramiko.transport:   File "C:\Users\caira\Anaconda3\lib\site-packages\paramiko\packet.py", line 622, in _read_timeout
ERR [20210202-17:54:50.028] thr=1   paramiko.transport:     raise socket.timeout()
ERR [20210202-17:54:50.028] thr=1   paramiko.transport: socket.timeout
ERR [20210202-17:54:50.028] thr=1   paramiko.transport: 
ERR [20210202-17:54:50.028] thr=1   paramiko.transport: During handling of the above exception, another exception occurred:
ERR [20210202-17:54:50.028] thr=1   paramiko.transport: 
ERR [20210202-17:54:50.029] thr=1   paramiko.transport: Traceback (most recent call last):
ERR [20210202-17:54:50.029] thr=1   paramiko.transport:   File "C:\Users\caira\Anaconda3\lib\site-packages\paramiko\transport.py", line 2039, in run
ERR [20210202-17:54:50.029] thr=1   paramiko.transport:     self._check_banner()
ERR [20210202-17:54:50.029] thr=1   paramiko.transport:   File "C:\Users\caira\Anaconda3\lib\site-packages\paramiko\transport.py", line 2216, in _check_banner
ERR [20210202-17:54:50.029] thr=1   paramiko.transport:     "Error reading SSH protocol banner" + str(e)
ERR [20210202-17:54:50.029] thr=1   paramiko.transport: paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
ERR [20210202-17:54:50.029] thr=1   paramiko.transport: 

Am i doing something obvious that is wrong?我在做一些明显错误的事情吗?

Should i be using the 'Public IPv4 DNS' instead?我应该改用“公共 IPv4 DNS”吗? => ec2-##-##-###-##.us-east-2.compute.amazonaws.com => ec2-##-##-###-##.us-east-2.compute.amazonaws.com

I am not sure if my username for above should be my full email address or my shorter username.我不确定我上面的用户名应该是我的完整 email 地址还是我较短的用户名。

It is possible you can use SCP or SFTP to connect to the server and copy the data to your local machine.您可以使用 SCP 或 SFTP 连接到服务器并将数据复制到本地计算机。 There are many ways to do that but for your use case you can use a library like Paramiko to handle the connection and the file transfer, if you like a sample code look at the answer https://stackoverflow.com/a/3635163/2156106有很多方法可以做到这一点,但对于您的用例,您可以使用 Paramiko 之类的库来处理连接和文件传输,如果您喜欢示例代码,请查看答案https://stackoverflow.com/a/3635163/ 2156106

Update: For EC2 instance, you need to use the private key you downloaded or provided while you created the instance as follows更新:对于 EC2 实例,您需要使用您在创建实例时下载或提供的私钥,如下所示

import paramiko

key = paramiko.RSAKey.from_private_key_file("path_to_key.pem")

transport = paramiko.Transport((host, port))
transport.connect(username="username", pkey=key)

sftp = paramiko.SFTPClient.from_transport(transport)
sftp.get(remote_path , local_path)

The username should be the username on the server.用户名应该是服务器上的用户名。 if it is an ubuntu server it is ubuntu by default in EC2.如果它是ubuntu服务器,则在 EC2 中默认为 ubuntu。

you can use scp or sftp to do that ( https://www.ssh.com/ssh/sftp/ ), but I'd strongly recommend sending those files to aws s3 from the server (or even stream it).您可以使用SCP或SFTP来执行此操作( https://WWW.Z1787D7646304C5987CF4CF4CF4CF4E64A4A4A3973DC7Z.COL828281C..com/SFTP/甚至是这些文件。

you could then use awscli ( https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html ) or python's boto3 ( https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-example-download-file.html ) to copy the files from s3 to your local directory you could then use awscli ( https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html ) or python's boto3 ( https://boto3.amazonaws.com/v1/documentation/api/ latest/guide/s3-example-download-file.html )将文件从 s3 复制到本地目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM