user1853777
user1853777

Reputation: 767

SailsJS User upload images

I need to let my users upload images. But I don't know where can I create my "uploads" folder and how have access to it in my view.

In SailsJS, we have a folder assets/images, but all images in it will be copied in the public folder .tmp, when we lift sails (with Grunt). So I can't create my folder in .tmp neither in asset/images.

Someone have a solution ? Thanks

Upvotes: 2

Views: 1426

Answers (3)

pradeep gowda
pradeep gowda

Reputation: 662

To Upload Images

There are two ways to upload images
  1. Upload images to Amazon s3 bucket.
  2. Upload Images mongodb gridFs.


I would like suggest to Uploading images to Aws (Amazon Web Services) S3 bucket why

 S3(Simple Storage Service)

Because S3 bucket is highly Scalable , relaible, fast and inexpensive simple data infrastructure to store.

Example AWS S3 bucket

Amazon provide a library/dependency for javascipt in NodeJs to access S3 bucket.

var AWS = require('aws-sdk');
var accessKeyId ="JHPSJFPDOKK4YLGMW25kLia";
var secretAccessKey ="kkl96vPMrc9rnDBSs5Yqq2cKMHlham5T2wfjBhj89H";
AWS.config.update({
    accessKeyId: accessKeyId,
    secretAccessKey: secretAccessKey
});

var s3 = new AWS.S3();

s3.createBucket({Bucket: 'myBucket'}, function() {
  var params = {Bucket: 'myBucket', Key: 'myKey', Body: 'Hello!'};
  s3.putObject(params, function(err, data) {
     if (err){
      console.log(err);  
     }else{
      console.log("Successfully uploaded data to myBucket/myKey")
     }
  })
});

Upvotes: 1

Abdul Vajid
Abdul Vajid

Reputation: 1411

You can you skipper module for file upload. In latest versions of Sails, Skipper is the default body parser, so you will be able to use it directly. If you are using Amazon S3, there is skipper-s3 module for uploading to s3. But the best practice woul be to upload the file directly without hitting your server, to any CDN like s3,cloudinary or something else via AJAX request. This will reduce the load on the server.

Upvotes: 0

bredikhin
bredikhin

Reputation: 9025

Assuming you are on a Linux-based system, you can just create your uploads folder in you project's root and symlink it from assets folder:

mkdir uploads && cd assets && ln -s ../uploads

Once Sails is lifted all the files from the uploads folder will be accessible via http://your.host.com/uploads/filename.ext, including the once you upload during runtime.

That said, for large applications in production with a lot of uploads it would make sense to upload files directly to Amazon S3 or something similar and serve them via a CDN.

Upvotes: 3

Related Questions