How do I provide a zip file for download from an A

2020-05-03 13:38发布

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.

0条回答
登录 后发表回答