NodeJS-上载到s3而不保存在本地文件系统中 [英] NodeJS - Upload to s3 without saving in local file system

查看:215
本文介绍了NodeJS-上载到s3而不保存在本地文件系统中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从REST API的POST响应中收到一些文本.我想为此直接在s3中创建一个文本文件.我偶然发现的所有示例都使用本地文件,然后将其上传.有什么方法可以直接上传而不保存在本地系统中吗?

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?

推荐答案

您可以直接将req传递给s3.upload函数,如下所示:

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") )

发布的文件将直接上传到AWS s3.

The posted file will be directly uploaded to aws s3.

这篇关于NodeJS-上载到s3而不保存在本地文件系统中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆