简体   繁体   English

如何在 NODE JS 中使用 AXIOS 将文件从远程 AWS S3 发送到其他服务器

[英]How can I send file from a remote AWS S3 to Other Server Using AXIOS in NODE JS

Hello I am implementing an API endpoint where I can send a file coming from aws S3.您好,我正在实现一个 API 端点,我可以在其中发送来自 aws S3 的文件。 What I have done is I get the file in from my s3 bucket我所做的是从我的 s3 存储桶中获取文件

let file_data = await S3.getObject(params).promise();
let buf = Buffer.from(file_data.Body);

const file = buf.toString("base64");

Now I want to send the file to other API for verification purposes.现在我想将文件发送到其他 API 以进行验证。 I tried something like this but doesn't seems to work我试过这样的事情,但似乎不起作用

var data = new FormData();

const data_file = fs.createReadStream(fs.createWriteStream(file));
data.append("document", data_file);

var config = {
    method: "post",
    url: `${process.env.URL}/verify_file`,
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        Authorization: `Bearer ${token}`,
        ...data.getHeaders(),
    },
    data: data,
};

try {
    const res = await axios(config);
    console.log(res.data);
} catch (error) {
    console.error("sendPassport", error);
}

I found my solution.我找到了我的解决方案。 I create a file from a buffer that I get from AWS S3.我从从 AWS S3 获得的缓冲区创建了一个文件。 Then save it in my current working directory.然后将其保存在我当前的工作目录中。

    let file_data = await S3.getObject(params).promise();

    const buffer = Buffer.from(file_data.Body, "utf8");
    // Create and download a file
    fs.writeFileSync(
                `${process.cwd()}/public/tmp/${data.filename}`,
                buffer
            );

Then I converted the file into stream using fs.createReadStream() function then send send the file然后我使用fs.createReadStream()函数将文件转换为流,然后发送发送文件

    var data = new FormData();

    const data_file = fs.createReadStream(`${process.cwd()}/public/tmp/${data.filename}`);

    data.append("document", data_file);

    var config = {
        method: "post",
        url: `${process.env.URL}/verify_file`,
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            Authorization: `Bearer ${token}`,
            ...data.getHeaders(),
        },
        data: data,
    };

    try {
        const res = await axios(config);
        console.log(res.data);

    } catch (error) {
        console.error("sendPassport", error);
    }

    // Delete the file
    fs.unlinkSync(file_path);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 AWS SDK(在 Node.js 中)中的 putObject() 将文件从 HTTP 响应正文上传到 S3? - How can I upload a file from an HTTP response body to S3 using putObject() from the AWS SDK (in Node.js)? 如何使用 Node.js 将文件从 AWS Lambda 中的 /tmp 文件夹上传到 S3 - How to upload a file to S3 from the /tmp folder in AWS Lambda using Node.js 如何使用 Node.js 的 AWS SDK 将 Amazon S3 中的所有对象从一个前缀复制/移动到另一个前缀 - How to copy/move all objects in Amazon S3 from one prefix to other using the AWS SDK for Node.js 从 AWS S3 到 Node.js 服务器的文件检索,然后到 React 客户端 - File retrieval from AWS S3 to Node.js server, and then to React client 使用节点js和Postman将文件上传到AWS S3 - Upload a file to AWS S3 using node js and Postman 如何将AWS S3文件缓存在node.js服务器上并根据请求提供服务? - How to cache an AWS S3 file on a node.js server and serve it on request? 如何从 React js axios 发布请求将 FormData 发送到节点服务器? - how do i send FormData to the node server from React js axios post request? 如何使用节点 js 通过 axios 将文件发送到外部 api - How to send a file via axios to an external api using node js 使用Node.js从EC2实例下载AWS S3文件 - Download AWS S3 file from EC2 instance using Node.js 如何使用 Node.js 和 Axios 将文件上传到 AWS 中的预签名 URL? - How do I upload a file to a pre-signed URL in AWS using Node.js and Axios?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM