Making a long story short, I would like to iterate in an array by using the array.map() function, and for each item in the array call an async API (from inside the array.map), but I know that this is not possible. Based on the scenario below, can you share your pro ideas and miraculous/magical solutions for this case with me?
SCENARIO:
-
I have images stored in a private S3 bucket in AWS, and my React application fetches the names of some of the images from a database and store them in an array. I need to display in the screen all the images in the array
-
As the images are in the private S3 bucket, I cannot simply use their paths (bucket name + the image name) as the source of the image element in the page (<img src=’aws.my-bucket.com.blablabla/image.jpg’ />). Instead, as per AWS guidelines, I have to generate a “pre-signed URL” for each image name, and this pre-signed URL is the one that is used for rendering the images (the API that generates this URL is ready, and If I get the URL generated by my API and paste it in the browser, I successfully see the image)
-
The tricky part is: how to loop into the array and call the API for generating the URL for each item based on the images names, if the API call has to be asynchronous, and it is not possible to call async functions from array.map()?
I thought of somehow doing it inside useEffect(), but couldn’t really visualize how to do so.
This is my current code, with the S3 path as the “src” of the <img/> element that is working because I made the S3 public. But I need to make it private and pass the pre-signed URL instead:
export default function Gallery() {
{/*
Some code that fetches the names of the images and stores them in the array
*/}
return (
<div>
{
gallery.map(image=>
(
<figure>
<div>
<img src={`https://s3-location-amazonaws.com/my-bucket-name/${image.FILE_NAME}`} /> // <--- Here I need to somehow use the URL returned by the app.get('/s3/:bucket/:key' , async (req, res) API described below
</div>
<figcaption>{image.LABEL}</figcaption>
</figure>
)
)
}
</div>
)
}
API in the back-end (node.js) that generates the URL needed for rendering the image:
app.get('/s3/:bucket/:key' , async (req, res) => {
try {
const clientUrl = await generatePreSignedUrl({
bucket: `${req.params.bucket}`,
key: `${req.params.key}`,
});
res.send(clientUrl);
} catch (err) {
console.error(err);
}
})
Function that generates the pre-signed URL in case you want to take a look at it:
async function generatePreSignedUrl({ bucket, key }) {
const { GetObjectCommand, S3Client } = require("@aws-sdk/client-s3");
const { getSignedUrl, S3RequestPresigner } = require("@aws-sdk/s3-request-presigner");
// No need to pass the object with the region and the credentials to the S3 as this parameters were configured in AWS CLI by running the AWS CONFIGURE command in both the EC2 instance and the dev environment
const client = new S3Client();
const command = new GetObjectCommand({ Bucket: bucket, Key: key });
return getSignedUrl(client, command);
}
I tried to call the API asynchronously from inside the array.map() but of course it didn’t work
GenerikError is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.