【问题标题】:AWS S3 uploading 0 B file - node.jsAWS S3上传0 B文件-node.js
【发布时间】:2020-03-15 16:05:30
【问题描述】:

我正在尝试使用 [putObject][1] 将文件上传到 AWS S3,但它会生成 0 字节大小的文件。

我确实从 putObject 调用中得到了成功的响应。

Node.js 代码:

const aws = require("aws-sdk");
const s3 = new aws.S3();

module.exports = {
  upload: function(req, res, next) {
    console.log("Going to upload");
    console.log(req.files);
    let uploadFile = req.files.file;
    const s3PutParams = {
      Bucket: process.env.S3_BUCKET_NAME,
      Key: uploadFile.name,
      Body: uploadFile.data,
      ACL: "public-read"
    };

    const s3GetParams = {
      Bucket: process.env.S3_BUCKET_NAME,
      Key: uploadFile.name
    };

    console.log(s3PutParams);
    s3.putObject(s3PutParams, function(err, response) {
      if (err) {
        console.error(err);
      } else {
        console.log("Response is", response);
        var url = s3.getSignedUrl("getObject", s3GetParams);
        console.log("The URL is", url);
        res.json({
          returnedUrl: url,
          publicUrl: `https://${process.env.S3_BUCKET_NAME}.s3.amazonaws.com/${uploadFile.name}`
        });
      }
    });
  }
};

通过 POSTMAN 测试:

后端控制台日志

谁能帮我找出问题所在?

11/20 编辑: @EmmanuelNK 帮助发现了 Buffer.byteLength(req.files.file.data) 为 0 的事实。他有以下问题:

您是想将整个缓冲区写入内存还是将其流式传输到 s3?

对不起,如果答案不是重点,我的脚仍然湿透了。 基本上我想将图像上传到 S3,然后使用该 URL 在网页上显示它。换句话说,就像一个照片桶

您如何使用上传

现在我只是使用邮递员测试我的后端代码(发布在问题中)。一旦我开始这样做,前端就会有一个文件上传表单调用这个路由。

这有帮助吗?提前感谢您的帮助。

【问题讨论】:

  • 那么它在 S3 存储桶中的 0 字节?缓冲区本身呢?我知道size 属性显示64476Buffer.byteLength(req.files.file.data) 呢?登录时显示什么?
  • @EmmanuelNK 当我登录 Buffer.byteLength(req.files.file.data) 时它显示为 0,是的,它在 S3 存储桶中为 0 个字节。
  • 这就是问题所在。你的缓冲区是空的。没有数据。所以问题可能在于您的上传代码。您是要尝试将整个缓冲区写入内存还是要将其流式传输到 s3?请更新您的问题,说明您使用上传的方式或使用位置。
  • @EmmanuelNK 我已经更新了问题
  • 您使用什么中间件进行文件上传?是express-fileupload 还是别的什么?另外,能否请您向我们展示您为中间件设置编写的代码。

标签: node.js amazon-web-services amazon-s3 file-upload


【解决方案1】:

如果您使用express-fileupload 作为文件上传中间件,并且您已将useTempFiles 选项设置为true,请记住您的数据文件缓冲区将为空(检查usage),这与对于您面临的问题。要解决这个问题,只需阅读温度。再次文件以获取预期的文件缓冲区。

import fs from 'fs';
// OR
const fs = require('fs');

// in your route
let uploadFile = req.files.file;
// THIS
fs.readFile(uploadedFile.tempFilePath, (err, uploadedData) => {
    if (err) { throw err; }

    const s3PutParams = {
        Bucket: process.env.S3_BUCKET_NAME,
        Key: uploadFile.name,
        Body: uploadData, // <--- THIS
        ACL: "public-read"
    };

    const s3GetParams = {
        Bucket: process.env.S3_BUCKET_NAME,
        Key: uploadFile.name
    };

    console.log(s3PutParams);
    s3.putObject(s3PutParams, function(err, response) {
        if (err) {
            console.error(err);
            throw err;
        } else {
            console.log("Response is", response);
            var url = s3.getSignedUrl("getObject", s3GetParams);
            console.log("The URL is", url);
            res.json({
                returnedUrl: url,
                publicUrl: `https://${process.env.S3_BUCKET_NAME}.s3.amazonaws.com/${uploadFile.name}`
            });
        }
    });
});

【讨论】:

    猜你喜欢
    • 2012-12-31
    • 2022-11-03
    • 2021-10-24
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 2016-04-07
    相关资源
    最近更新 更多