This example shows you how to implement a smart action to upload image files to an AWS S3 bucket.
Here we have a companies model that has two fields corresponding to files stored in an AWS S3 bucket:
A certificate of incorporation
A proof of identity
We implemented a smart action to upload the files for each company.
Requirements
An admin backend running on forest-express-sequelize
This directory contains the companies.js file where the smart action Upload Legal Docs is declared.
You need to specify that the widget file picker is applicable to the input field used to upload the file.
companies.js
const { collection } =require('forest-express-sequelize');collection('companies', { actions: [ { name:'Upload Legal Docs', type:'single', fields: [{ field:'Certificate of Incorporation', description:'The legal document relating to the formation of a company or corporation.', type:'File', isRequired:true, }, { field:'Valid proof of ID', description: 'ID card or passport if the document has been issued in the EU, EFTA, or EEA / ID card or passport + resident permit or driving licence if the document has been issued outside the EU, EFTA, or EEA of the legal representative of your company',
type:'File', isRequired:true, }], }, ], fields: [], segments: [],});
Directory: /services
This directory contains an s3-helper.js file where the methods to upload files to s3 is declared.
You need to configure your AWS credentials inside your app to get access to your bucket. You can read more about it in the AWS documentation here.
This directory contains the companies.js file where the logic of the smart action is implemented.
companies.js
constexpress=require('express');const { PermissionMiddlewareCreator } =require('forest-express-sequelize');const { companies } =require('../models');constuuid=require('uuid/v4');constS3Helper=require('../services/s3-helper');constP=require('bluebird');constrouter=express.Router();constpermissionMiddlewareCreator=newPermissionMiddlewareCreator('companies');functionuploadLegalDoc(companyId, doc, field) {constid=uuid();//upload the files using the helperreturnnewS3Helper().upload(doc,`livedemo/legal/${id}`)//once the file is uploaded, update the relevant company field with the file id.then(() =>companies.findByPk(companyId)).then((company) => { company[field] = id;returncompany.save(); }).catch((e) => e);}router.post('/actions/upload-legal-docs',permissionMiddlewareCreator.smartAction(), (req, res) => {// Get the current company idconstcompanyId=req.body.data.attributes.ids[0];// Get the values of the input fields entered by the admin user.constattrs=req.body.data.attributes.values;constcertificateOfIncorporation= attrs['Certificate of Incorporation'];constpassportId= attrs['Valid proof of ID'];P.all([uploadLegalDoc(companyId, certificateOfIncorporation,'certificateOfIncorporationId'),uploadLegalDoc(companyId, passportId,'passportId'), ]).then(() => {// Once the upload is finished, send a success message to the admin user in the UI.returnres.send({ success:'Legal documents are successfully uploaded.' }); });});module.exports= router;