gpt4 book ai didi

node.js - 使用 Node.js API 上传到 Amazon S3 速度极慢

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:59 24 4
gpt4 key购买 nike

一直试图找出为什么使用 putObject 命令(node.js 库)上传到 Amazon S3 的速度慢得惊人。下面的代码读取整个目录的文件并将它们异步放入 S3。

//Read a directory of files
fs.readdir(dir,function(err,files){

//Read each file within the folder
for(var i=0; i < files.length; i++){

var file = files[i];

//Read the File
fs.readFile(path.join(dir,file), function(err, data){

//Create a new buffer
var buffer = new Buffer(data, 'base64');

//Add the pdf to S3
s3.putObject({
'Bucket':bucket,
'Key':path.join(key,file),
'Body':buffer,
'ContentType':mime(file)
},function(err, data) {

//Wait for all the other files to be done
// and perform a callback
});
});
}
});

使用多个不同的文件夹进行测试,结果相似。

  • 6 个文件大小均在 1-2 Kb 之间,1 个 @ 63Kb 除外(上传时间超过 20 秒)
  • 4 个文件全部大小为 3kb(上传时间超过 20 秒)

使用AWS web interface上传相同的文件大约需要 3 秒(或更短)才能完成。为什么使用node.js API这么慢?

根据亚马逊文档,我什至尝试生成多个子项来独立处理每个上传。上传速度没有变化。

最佳答案

您在 Node 中创建新的 S3 实例时是否设置了正确的区域

例如,您的 s3 存储桶位于 us-east-1 中。为了获得最佳传输速度,您需要确保您的 S3 实例设置为该区域,例如:

const s3 = new AWS.S3({
accessKeyId: "xxx",
secretAccessKey: "xxx",
region: 'us-east-1'
});

否则它可能会非常慢。有人可能会插话发生这种情况的具体原因——我猜这与在执行多部分请求时必须不断查找实际区域有关,或者可能上传到距离更远的另一个区域有关您的目的地地区。

关于node.js - 使用 Node.js API 上传到 Amazon S3 速度极慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25988701/

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