gpt4 book ai didi

javascript - 如何找到我刚刚上传到 S3 的流的总大小?

转载 作者:行者123 更新时间:2023-11-30 19:25:25 25 4
gpt4 key购买 nike

在 Node.js 服务器上,我正在将流上传到 S3。这是一个简化的示例:

import S3 from "aws-sdk/clients/s3"

const s3 = new S3({ accessKeyId, secretAccessKey, region })

async function uploadStream(key, stream) {
const upload = s3.upload({
Body: stream,
Bucket: clientFiles,
Key: key,
})

const result = await upload.promise()
return result // { Bucket, ETag, Key, Location }
}

由于我使用的是流,所以在开始上传时我不知道总大小,上传结果除了 Bucket、ETag、Key 和 Location 值之外什么也没有给我。

如何获取上传流的总大小?

最佳答案

使用 ManagedUploadhttpUploadProgress 怎么样?
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3/ManagedUpload.html#httpUploadProgress-event

async function uploadStream(key, stream) {
const upload = new AWS.S3.ManagedUpload({
params: {
Body: stream,
Bucket: clientFiles,
Key: key
}
});
upload.on("httpUploadProgress", function(progress) {
console.log(progress.total);
});
...

关于javascript - 如何找到我刚刚上传到 S3 的流的总大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56961459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com