{ sailsit }

Sails Js Tutorials for Beginners and Forum

Have a Question?

If you have any question you can ask below or enter what you are looking for!

Upload Files – AWS S3

1. Creating SailsJs Project

Follow the below tutorial for setting up SailsJs
Setting up sails project

2. Creating Controller

Use the below command to generate a controller

sails generate controller Upload

paste the below code in api/controllers/UploadController.js

/**
 * UploadController
 *
 * @description :: Server-side logic for managing Uploads
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */


const S3_KEY = "YOUR_S3_KEY";
const S3_SECRET = "YOUR_S3_SECRET";
const S3_BUCKET = "YOUR_S3_BUCKET";

module.exports = {

        upload: function (req, res) {
//avatar: key name of the file in the paylad
                req.file('avatar').upload({
                adapter: require('skipper-s3'),
                key: S3_KEY,
                secret: 'S3_SECRET',
                bucket: 'S3_BUCKET'
                }, function (err, filesUploaded) {
                        if (err) return res.negotiate(err);
                        return res.ok({
                                files: filesUploaded
                        });
                });
        }

};

 

3. Installing Packages

For uploading files to AWS S3 bucket, we need to install s3-skipper package, this package is available in npm.
To install the package use the below command

npm install skipper-s3 --save

 

4. Creating Route

Let’s create a route for triggering the controller function
Paste the below route in your config/routes.js

'POST /api/aws/s3/upload': 'UploadController.upload',

 

5. Running Sails

Now you can start your sails project using below command

sails lift

 
You can find the source code for the tutorial at below URL
upload_files_S3