Search code examples
pythonazureflaskazure-storagechatbot

Share file from python-flask app from azure app service


I've a python-flask webapp which is deployed on Azure which serves as backend for a chatbot. Based on the user query, the bot performs an action and saves the file locally. This file needs to be shared to the user.

Is there a way that I can share a hyperlink to the user in chat for the user to download the file without having to create a storage on azure. (The file need not be stored and can be deleted after a timeout. File sizes are very small(10-20 kB))


Solution

  • You can serve a file with a Flask endpoint using send_from_directory

    You could save the file with a name that is a hash or the file (or any unique naming convention), and use that as a link for users to use. Suppose you keep these files in the FILE_DIR directory.

    from flask import request, send_from_directory
    
    @app.route('/some-endpoint/')
    def download_file():
        filename = request.args.get('hash')
        return send_from_directory(FILE_DIR, filename, as_attachment=True)