Need to preface this question with the fact that I am a total newcomer to Javascript. I know that the below code is filled with errors but it manages to at least fetch the presigned URLS and push them to the array allURLs. However when I return they are not available in the array allUrls and I get the following error message:
ReferenceError: allURLs is not defined
Goal: Fetch all keys for images in AWS bucket, loop over said keys and return presigned URLs to array that is available to map on page.
Below is the code I have been working with so far:
export default function Home ({ allURLs }) {
return (
<div id="test">
{allURLs &&
allURLs['address'].map((allURLs) => (
<h2>{allURLs.address}</h2>
))}
</div>
)
}
export async function getServerSideProps() {
const AWS = require('aws-sdk');
const s3 = new AWS.S3({
region: 'region',
credentials: {
accessKeyId: "Access Key",
secretAccessKey: "Secret Access Key"
}});
s3.listObjectsV2({
Bucket: 'Bucket Name'
}, (err, data) => {
if (err) reject(err);
const keys = data.Contents;
console.log(keys)
const allURLs = [];
for (let content of keys) {
let params = {
Bucket: 'Bucket Name',
Key: content.Key,
Expires: 2000
};
s3.getSignedUrl('getObject', params, (err, url) => {
var links = {};
links.address = url;
allURLs.push(links);
console.log(allURLs)
});
};
});
return {
props: { allURLs }
}
};