1
export async function ss3ListAllFilesTags(bucket: string, folder: string, tag: string){
  let params = {
    Bucket: `${TypesOptions[bucket].bucket}`,
    Prefix: `${folder}`,
    // MaxKeys: 1000,
  };

   return  s3.listObjectsV2(params, function(err, data){
    var tab: string[] = [], t : any;

    if (err) return err;
    else {
      Object.entries(data.Contents).forEach(  (element, cpt ) => {
          serviceDownloadFileWithTags(bucket,element[1].Key).then( (dat) => {
          dat= JSON.parse(dat);
          if ( dat['TagSet'][0].Value == tag || dat['TagSet'][1].Value == tag){
            tab.push(element[1].Key);
            console.log(tab)
            // console.log(`
            // nom fichier: ${element[1].Key}
            // tag 1 : ${JSON.stringify(dat['TagSet'][0])}, cle 1: ${JSON.stringify(dat['TagSet'][0].Key)}, valeur 1: ${JSON.stringify(dat['TagSet'][0].Value)}
            // tag 2 : ${JSON.stringify(dat['TagSet'][1])}, cle 2: ${JSON.stringify(dat['TagSet'][1].Key)}, valeur 2: ${JSON.stringify(dat['TagSet'][1].Value)}
            // `);
          }
        });

      });

      console.table(tab);
      return tab;
      // return data.Contents;
    }
  }).promise()
    
}

I am trying to write a asynchronous function in typescript. Function calls an s3 function to get list of the objects. How can I return tab as result of ss3ListAllFilesTags function.

1
  • i want to returnn filtered variable tab as result of the ss3ListAllFilesTags function Commented Jan 17, 2022 at 5:17

1 Answer 1

1

You can use async/await and Promise.all() like this:

export async function ss3ListAllFilesTags(bucket: string, folder: string, tag: string){
  let params = {
    Bucket: `${TypesOptions[bucket].bucket}`,
    Prefix: `${folder}`,
    // MaxKeys: 1000,
  };
  try {
    const data = await s3.listObjectsV2(params).promise()
    var tab: string[] = [], t: any;

    await Promise.all(
      Object.entries(data.Contents).map((element) =>
        serviceDownloadFileWithTags(bucket, element[1].Key).then((dat) => {
          dat = JSON.parse(dat)
          if (dat['TagSet'][0].Value == tag || dat['TagSet'][1].Value == tag) {
            tab.push(element[1].Key)
            console.log(tab)
          }
        })
      )
    )

    console.table(tab);
    return tab;
  } catch (err) {
    return err
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

i get data before the loop, but problem still remain, the ss3ListAllFilesTags didnt wait tab to bie fill before return ; it return empty and fill data after returned
Got it. Let me update my answer
your are a genuis, you save my week, may god give your reward, thank it work fine
i did it ,hope it is ok
i did it ,hope it is ok

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.