0

I am uploading video file using s3 bucket in Nodejs.

when I upload file on s3 bucket, it is also uploaded in temp/ folder. So after few video upload temp folder is become full due to this I can not upload more video.

How to remove this video from temp folder after file successfully or not upload on s3 bucket in Nodejs code?

I have used below package for s3 bucket. https://www.npmjs.com/package/s3

6
  • It's weird, there's no reason the file should be saved to a local directory without an explicit instruction to do so. As a workaround, you can always delete the temp folder after an upload Commented Feb 19, 2018 at 9:36
  • @JeremyThille I have no code for save file in temp folder Commented Feb 19, 2018 at 9:40
  • I think you should try fs.unlink Commented Feb 19, 2018 at 9:40
  • @sidgujrathi where I can try this ? I am beginner in nodejs. Commented Feb 19, 2018 at 9:41
  • See my answer below Commented Feb 19, 2018 at 10:14

2 Answers 2

2
var s3 = new AWS.S3();
s3.abortMultipartUpload(params, function (err, data) {
  if (err) console.log(err, err.stack); // USE BELOW CODE HERE
  else     console.log(data);           // ALSO HERE
});

Here is example of how can you remove/delete file from system using NodeJS

// include node fs module
var fs = require('fs');

// delete file named 'sample.txt'
fs.unlink('sample.txt', function (err) {
    if (err) throw err;
    // if no error, file has been deleted successfully
    console.log('File deleted!');
}); 
Sign up to request clarification or add additional context in comments.

2 Comments

I know how to use this. But where to use it ?
See my edited answer, look for comment in IF and ELSE You can use code in that sections. So that if s3 successfully saves file it will delete from system and evenn if s3 fail to do so it will remove the temp file.
0
const multer = require("multer");


require("dotenv").config();

const aws = require("aws-sdk");
const multerS3 = require("multer-s3");

aws.config.update({
  region: process.env.AWS_BUCKET_REGION,
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_KEY,
});

const BUCKET = process.env.AWS_BUCKET_NAME;
const s3 = new aws.S3();

// Multer config
module.exports = multer({
  storage: multerS3({
    bucket: BUCKET,
    s3: s3,
    contentType: multerS3.AUTO_CONTENT_TYPE,
    acl: "public-read",

    key: function (req, file, cb) {
      cb(null, Date.now() + file.originalname); //use Date.now() for unique file keys
    },
  }),
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.