微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 s3 aws sdk 上传文件时写入结束错误

如何解决使用 s3 aws sdk 上传文件时写入结束错误

我在 node.js 中使用 s3 (aws sdk) 将文件/图像上传到 Linode 对象存储。 对于非常小的文件(小于 1 MB)工作正常,但是当文件大于 1MB 时会引发以下错误

error Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_http_outgoing.js:643:15)
at ClientRequest.end (_http_outgoing.js:763:7)
at features.constructor.writeBody ({pathtoproject}/node_modules/aws-sdk/lib/http/node.js:137:14)
at ClientRequest.<anonymous> ({pathtoproject}/node_modules/aws-sdk/lib/http/node.js:102:14)
at ClientRequest.emit (events.js:315:20)
at ClientRequest.EventEmitter.emit (domain.js:485:12)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:605:11)
at HTTPParser.parserOnHeadersComplete (_http_common.js:117:17)
at TLSSocket.socketonData (_http_client.js:507:22)
at TLSSocket.emit (events.js:315:20)

这是我的上传功能

const s3 = require('./index')
const { v4: uuidv4 } = require('uuid');

const uploadFile = (file,bucket) => {
const params = {
    Bucket: bucket,// pass your bucket name
    Key: `${uuidv4()}-${file.originalname}`,// file will be saved as testBucket/contacts.csv
    Body: file.buffer,ContentType: file.mimetype,ACL: 'public-read'
};
return new Promise((resolve,reject) => {
    s3.upload(params,(s3Err,data) => {
        if (s3Err) {
            reject(s3Err)
        } else {
            console.log(`Image uploaded at: ${data}`)
            resolve(data.Location)
        }
    });
 })
}  


module.exports = uploadFile

这是主要配置

var AWS = require("aws-sdk");

AWS.config.loadFromPath('./aws/config.json');

const s3 = new AWS.S3({ httpOptions: { timeout: 10 * 60 * 1000 }});

module.exports = s3

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。