OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How could I List all the CommonPrefixes first with listObjectsV2?

  • Thread starter Thread starter Eduardo Lemus Laguna
  • Start date Start date
E

Eduardo Lemus Laguna

Guest
I have a lambda function requesting data from s3 using AWS-SDK, it is consumend by a webapp used as file directory, but when the app request to create a new folder/directory it doesn't shows until the user click several times on the next pages using the StartAfter value I send on the response, the bucket has around 24000 files and I need to send all the folders at the begining of the request to show'em on the view first (the front end sort the CommonPrefixes at the begining of the page as directories on the view)

I already list the objects on this way

Code:
module.exports.getDirectory = async (bucketPath = "", StartAfter = null) => {
    await initS3();
    const params = {
        Bucket: bucketName,
        Prefix: bucketPath,
        Delimiter: "/",
        MaxKeys: 300
    };
    try {
        if(StartAfter) params.StartAfter = StartAfter;
        console.log(JSON.stringify(params));
        const data = await s3.listObjectsV2(params).promise();
        return {
            content: data.Contents, 
            Prefix: data.Prefix, 
            CommonPrefixes: data.CommonPrefixes, 
            IsTruncated: data.IsTruncated
        };
    } catch (err) {
        console.log(err);
        return {err: err};
    }
}

Do I neet to sort the folder when it is created or there's a way to get all of them when I request the objects the first time the service is consumed?

<p>I have a lambda function requesting data from s3 using AWS-SDK, it is consumend by a webapp used as file directory, but when the app request to create a new folder/directory it doesn't shows until the user click several times on the next pages using the StartAfter value I send on the response, the bucket has around 24000 files and I need to send all the folders at the begining of the request to show'em on the view first (the front end sort the CommonPrefixes at the begining of the page as directories on the view)</p>
<p>I already list the objects on this way</p>
<pre><code>module.exports.getDirectory = async (bucketPath = "", StartAfter = null) => {
await initS3();
const params = {
Bucket: bucketName,
Prefix: bucketPath,
Delimiter: "/",
MaxKeys: 300
};
try {
if(StartAfter) params.StartAfter = StartAfter;
console.log(JSON.stringify(params));
const data = await s3.listObjectsV2(params).promise();
return {
content: data.Contents,
Prefix: data.Prefix,
CommonPrefixes: data.CommonPrefixes,
IsTruncated: data.IsTruncated
};
} catch (err) {
console.log(err);
return {err: err};
}
}
</code></pre>
<p>Do I neet to sort the folder when it is created or there's a way to get all of them when I request the objects the first time the service is consumed?</p>
 

Latest posts

D
Replies
0
Views
1
Dhanushka Amarakoon
D
S
Replies
0
Views
1
Shikhar Ambashta
S
Top