0

要将文件上传到 S3 存储桶中,我使用以下函数进行了管理,我将直接从另一个 URL 上传文件,为此我使用了node-fetchpackage.json 文件。

public async uploadFile(imageCount) {
  try {
    const count = imageCount
    console.log("count A- ", count) // let's say, 100

    return new Promise(async (resolve) => { 

    const response = await fetch(url),
          buffer = await response.buffer();

    console.log("count B- ", count) // now value becomes any random value i.e, 80 or 20 or 120 etc

      const resolution = [
        { key: 'thumbnail', value: { height: 256, width: 256 }},
        { key: 'small', value: { height: 72, width: 72 }},
      ]
      resolution.forEach(size => {
        Sharp(buffer)
        .resize(size.value).toBuffer()
        .then(async data => {
          var object = {
            Body: data,
            Bucket: ENV.BUCKET_NAME,
            Key: `${size.key}/${imglabel}/${keys}.${fileExt}`,
            ServerSideEncryption: "AES256",
          };
          bulkOfBody.push(object)
          responses = await Promise.all(
            bulkOfBody.map(param => s3.upload(param).promise())
          )
          if (responses.length === 2) {
            resolve(responses);
          }
        })
      })
    })
  } catch (error) {
    throw error;
  }
}

现在,奇怪的是,在count A日志位置,计数变得完美,但是当我执行计数时response.buffer(),计数将变为任何随机值。

我可以知道原因吗?

4

0 回答 0