Soumyajit Dutta
Soumyajit Dutta

Reputation: 308

NodeJS - Upload to s3 without saving in local file system

I am receiving some text from a POST response in a REST API. I want to directly make a text file in s3 for that. All the examples I have stumble upon are using a local file and then uploading it. Is there any way to directly upload it without saving in the Local System?

Upvotes: 4

Views: 4229

Answers (1)

Malice
Malice

Reputation: 1482

You can directly pipe the req to s3.upload function as below

import express from 'express';

import {S3} from 'aws-sdk';

const s3 = new S3();
const app = express();

app.post('/data', async (req, res) => {
    var params = {
        Body: req,
        Bucket: "yourbucketname here",
        Key: "exampleobject",
    };
    s3.upload(params, async (err, data) => {
        if(err)
            log.info(err);
        else
            log.info("success");
    });
});
const server = app.listen(8081,async () => log.info("App listening") )

The posted file will be directly uploaded to aws s3.

Upvotes: 3

Related Questions