简体   繁体   English

使用循环生成不同的签名 url (S3)

[英]Generating different signed urls with a loop (S3)

im having an issue trying to create a list of urls for multipart upload to S3 using lamba, im trying to generate the URLs using a loop in the lambda function, but it only generates 1 and keeps returning that one across all the loop, so at the end i have an array of urls with the same url repeated, it doesnt happen if i call the generateSignedUrl() method multiple times in the code, i mean if instead of calling the for loop to loop 3 times, i use the generateSignedUrl() method 3 times it actually generates 3 different URLs, while the loop generates 1 repeated 3 times.我在尝试使用 Lamba 创建用于分段上传到 S3 的 URL 列表时遇到问题,我尝试使用 lambda 函数中的循环生成 URL,但它只生成 1 并在整个循环中不断返回该 URL,所以在最后我有一个重复相同 url 的 url 数组,如果我在代码中多次调用 generateSignedUrl() 方法不会发生这种情况,我的意思是如果不是调用 for 循环来循环 3 次,我使用 generateSignedUrl( ) 方法 3 次它实际上生成 3 个不同的 URL,而循环生成 1 个重复 3 次。

async function getSignedUrl(key){
    return new Promise((resolve,reject) => {
      let params = { Bucket: bucketName, Key: key };
      s3.getSignedUrl('getObject', params, (err, url) => {
        if (err) reject(err)
        resolve(url);
      })
});
}

async function process(items) {
  for (let item of items) {
    const signedUrl = await getSignedUrl(item.fileName);
    item.url = signedUrl;
  }
  return items;
}


process(result).then(res => {
  console.log(res);
});

Inside your for loop you are declaring signedUrl as a const.在 for 循环中,您将signedUrl声明为常量。 This is why the value of the signedUrl is not changing for the items in the for loop and why it is working when you are calling your getSignedURL method explicitly.这就是为什么 for 循环中的项目的signedUrl的值没有改变的原因,以及当您显式调用getSignedURL方法时它可以工作的原因。

Try changing signedUrl to a var.尝试将signedUrl更改为 var。

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

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