How do I provide a zip file for download from an AWS EC2 instance running Ubuntu?

拜拜、爱过 提交于 2020-04-07 08:31:28

问题


I have created a Flask app that is capable of uploading multiple files to the EC2 instance. My flass app code:

import os
from flask import Flask, request, render_template, url_for, redirect
import flask
#Initialize the app by adding:

app = Flask(__name__)

#Add the below line to create the home page route:

@app.route("/")
def fileFrontPage():
    return render_template('index.html')


@app.route("/handleUpload", methods=['POST'])
def handleFileUpload():
    files = flask.request.files.getlist("photo")
    protocol = 'pscp -i'
    key_loc = ' key_loc'
    upload_dir = ' dir'
    ec2_instance = ' instance_name'
    remote_dir = ':/home/RAW_Image'
    for file in files:
        arg = protocol+key_loc+upload_dir+file.filename+ec2_instance+remote_dir
        os.system(arg)
    return redirect(url_for('fileFrontPage'))

if __name__ == '__main__':
    app.run()

Once the upload finishes, a program on the EC2 instance will compress the files and add them to a new directory. The new directory will be compressed and should be available for download once the download button is clicked (Haven't written the code for this yet). Once the upload button is pressed, another webpage will open with a download option.

This is what I have for creating a zip file and:

import paramiko
k = paramiko.RSAKey.from_private_key_file("key_location")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print("connecting")
c.connect(hostname="name", username="ubuntu", pkey=k)
print("connected")
commands = ["zip -r /home/Image.zip /home/RAW_Image", "scp username@remote:/file/to/send /where/to/put"]
for command in commands:
    print("Executing {0}".format(command))
    stdin , stdout, stderr = c.exec_command(command)
    print(stdout.read())
    print("Errors")
    print(stderr.read())
c.close()

But the scp gives a Host key verification failed error

Am I doing something wrong? Is there an alternative to this?

Thanks.

Edit 1: I am running the code on my personal computer. I am using ssh because I want to run a perform image recognition on the uploaded images and I was planning to execute a bash script that runs a bunch of Python scripts once the upload is done.

来源:https://stackoverflow.com/questions/60922885/how-do-i-provide-a-zip-file-for-download-from-an-aws-ec2-instance-running-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!