不能使用boto将SCP文件发送到AWS

时间:2015-12-16 17:21:50

标签: python amazon-ec2 boto

我正在尝试使用python自动上传到我的AWS EC2服务器。我无法让ssh_client.put_file()工作。它要么继续给我IOERROR: FailireIOError: [Errno 2] No such file

关于我缺少什么的任何想法?这个ssh_client不能用于scp上传吗?

import boto
import boto.ec2
from boto.manage.cmdshell import sshclient_from_instance
import argparse

#Parse input
parser = argparse.ArgumentParser(description='Upload and train images for detection')
parser.add_argument('path_to_key', help='Path to Pem key')
parser.add_argument('path_to_tar', help='Path to positives.tar')
args = parser.parse_args()
args_keypath = args.path_to_key
args_tarpath = args.path_to_tar

# Connect to your region of choice
    print "Connecting to server..."
    access_key = ""
    secret_access_key = ""
    conn = boto.ec2.connect_to_region('us-east-1', aws_access_key_id=access_key, aws_secret_access_key=secret_access_key)

    print "Connecting to instance..."
    # Connect to an existing instance
    reservations = conn.get_all_instances(['i-c8aab576'])
    instance = reservations[0].instances[0]

    # Create an SSH client for our instance
    #    key_path is the path to the SSH private key associated with instance
    #    user_name is the user to login as on the instance (e.g. ubuntu, ec2-user, etc.)
    print "Creating CommandShell..."
    key_path = args_keypath
    ssh_client = boto.manage.cmdshell.sshclient_from_instance(instance,
                                               key_path,
                                               host_key_file='~/.ssh/known_hosts',
                                               user_name='ubuntu')

    status, stdout, stderr = ssh_client.run('ls -al')
    print(status)
    print(stdout)
    print(stderr)

    #Upload positives - WELL THIS ISN'T WORKING
    print "Uploading file..."
    local_filepath = args_tarpath
    remote_filepath = "~/Sharing/"
    ssh_client.put_file("/home/willem/.ssh/test.txt", "/home/ubuntu/Sharing/")
    #ssh_client.put_file(local_filepath, remote_filepath)

2 个答案:

答案 0 :(得分:3)

如果你有ssh登录访问权限,你可以使用.pem并在本地运行命令,我的解决方案是:

通过创建预订来创建服务器:

reservation = conn.run_instances(my_AMI,
        key_name=my_key,
        instance_type='c4.xlarge',
        security_group_ids=security_group,
        placement='us-east-1d')

instance = reservation.instances[0]
print colored("Instance IP: %s" % instance.ip_address, 'yellow')

然后我可以scp文件:

instance_IP = instance.ip_address
os.system('scp -i %s %s ubuntu@%s:~/Sharing' % (key_path, args_tarpath, instance_IP) )

答案 1 :(得分:1)

一个简单的解决方案:也可以在目标路径中指定文件名。 Boto反过来使用paramiko模块,其中sftp.put需要指定文件名。