back S3 Storage

March 1, 2023

S3 Storage

This articles demonstrates how to upload files to a Spaces from Digital Ocean.

Requirements

  • Digital Ocean account
  • Spaces Subsciription

Big Picture

We will implement the file-upload on a ExpressJS server. We’ll use Multer to apply a middleware to get informations about the uploaded file via a form. Then we’ll upload the file with use of the aws-sdk package to Digital Ocean.

Implementation

First, setup the controller for your routes

1
2
3
4
5
6
7
8
const express = require('express');
const Router = express.Router;
const router = new Router();
var multer = require('multer');

router.post('/create', multer().single('document'), require('./controller/create'));

module.exports = router;

Notice that first we bring in the Multer package wich then is applied to the route as a middleware. The document in the brackets is the name of the input field from the form.

1
2
3
	<form>
		                <InputFile className="form__input" label="Document" value={ document.value } name="document" onChange={ (event) => setDocument({ ...document, filename: event.target.files[0].name, value: event.target.files[0] }) }  />
	</form>