📅  最后修改于: 2023-12-03 15:40:48.737000             🧑  作者: Mango
本文主要介绍Python中如何通过烧瓶(Flask)框架来实现将文件上传到保管箱(Dropbox)的操作。
from flask import Flask, request, redirect, url_for
import dropbox
import os
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
filename = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file', filename=filename))
return '''
<!doctype html>
<html>
<head>
<title>Upload new File</title>
</head>
<body>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
</body>
</html>
'''
app.config['UPLOAD_FOLDER'] = 'uploads'
@app.route('/uploaded_file')
def uploaded_file():
filename = request.args['filename']
upload_to_dropbox(filename)
return 'file uploaded successfully to dropbox'
def upload_to_dropbox(filename):
access_token = 'your_dropbox_access_token'
client = dropbox.Dropbox(access_token)
with open('uploads/' + filename, 'rb') as f:
client.files_upload(f.read(), '/' + filename)
if __name__ == '__main__':
app.run(debug=True)
通过以上步骤,我们完成了在Python中使用烧瓶框架实现将文件上传到Dropbox的操作。当然,我们也可以根据需要对程序进行自定义的修改和扩展。