Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Falcon and falcon-multipart + POST request for uploading files implementation

I'm trying to implement POST request for uploading files with Falcon framework (python).

I have used falcon-multipart in order to multipart/form-data, this allow me to retrieve my file in a cgi.FieldStorage() in which file is in binary format, but now, I need to write this file in a directory with the original extension.

This is the code I'm using.

app.py:

import falcon

from .files import Resource

from falcon_multipart.middleware import MultipartMiddleware

api = application = falcon.API(middleware=[MultipartMiddleware()])

files = Resource()
api.add_route('/files', files)

files.py:

import io
import os
import shutil

import falcon
import json


class Resource(object):

   _storage_path = './uploaded_files'

   def on_post(self, req, resp):
        """
        POST METHOD
        """
        # Retrieve file extension
        ext = req.get_param('extension')

        # Retrieve input_file
        input_file = req.get_param('file')

        # Read file as binary
        raw = input_file.file.read()

        # Retrieve filename
        filename = input_file.filename

        # Define file_path
        file_path = os.path.join(self._storage_path, filename)

        # Write to a temporary file to prevent incomplete files from
        # being used.
        temp_file_path = file_path + '~'

        # Finally write the data to a temporary file
        with open(temp_file_path, 'wb') as output_file:
            shutil.copyfileobj(raw, output_file)

        # Now that we know the file has been fully saved to disk
        # move it into place.
        os.rename(temp_file_path, file_path)

        resp.status = falcon.HTTP_201
like image 397
CAMILO HG Avatar asked Feb 23 '18 03:02

CAMILO HG


People also ask

What is a multipart Upload request?

A multipart upload request allows you to send metadata along with the data to upload. Use this option if the data you send is small enough to upload again in its entirety if the connection fails.

How do I make a multipart File Upload request in Postman?

In Postman all requests should have one header named “Content-Type” with value “multipart/form-data”. You can check the following screenshot, for setting this header value. spring boot multipart file upload postman headers Example 1: Spring boot multipart file upload example

Is it possible to post a file using multipart/form-data body?

What's throwing me off is their mention in above link for the "data" field : "Note that it's only possible to post a file using a multipart/form-data body (see RFC 2388)." I can't figure out how to format my JSON request in this multipart/form-data format that the API will understand correctly. Everything I upload comes through as corrupted.

How do I use multipart file in Spring Boot?

Create a Rest API in spring boot which consumes the multipart request data. The rest api method should have an input parameter for multipartFile, which will be automatically mapped by Spring. We need to take care of the file parameter’s name, with this same name we will be sending api requests.


1 Answers

I had to study cgi

  • cgi - File upload
  • cgi - Big file upload

This is the implementation I used:

def on_post(self, req, resp):
        """
        POST METHOD
        """
        # Retrieve input_file
        input_file = req.get_param('file')

        # Test if the file was uploaded
        if input_file.filename:
            # Retrieve filename
            filename = input_file.filename

            # Define file_path
            file_path = os.path.join(self._storage_path, filename)

            # Write to a temporary file to prevent incomplete files
            # from being used.
            temp_file_path = file_path + '~'

            open(temp_file_path, 'wb').write(input_file.file.read())

            # Now that we know the file has been fully saved to disk
            # move it into place.
            os.rename(temp_file_path, file_path)

    resp.status = falcon.HTTP_201
like image 199
CAMILO HG Avatar answered Sep 28 '22 02:09

CAMILO HG