简体   繁体   English

如何在nodeJs + sequelize + graphql中的s3存储桶上上传之前获取文件大小?

[英]How to get file size before upload on s3 bucket in nodeJs + sequelize+graphql?

Validate file size before upload on s3 bucket.currently use this code for get file size:在上传到 s3 存储桶之前验证文件大小。当前使用此代码获取文件大小:

const {
  createReadStream,
  filename
} = await file
const stream = await createReadStream()
let chunk = [];
let conLength;
stream.on('data', data => chunk.push(data))
  .on('end', () => {
    const buffer = Buffer.concat(chunk);
    conLength = buffer.length;
  });
console.log(conLength)
const url = await uploadToS3(stream, filename)
assetRequestUrls.push(url)

But file size get in conLength variable that use only in function not used outside function.但是文件大小进入 conLength 变量,仅在 function 中使用,不在 function 之外使用。 So, please help me.所以,请帮助我。

If you want to console.log the filesize如果你想console.log文件大小

const fs = require('fs')

const { size } = fs.statSync(filename)
console.log(size)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM