'upload file in Nodejs using s3 npm package
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
Solution 1:[1]
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!');
});
Solution 2:[2]
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
},
}),
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | dammika rajapaksha |