白板的微信
2022-05-14 15:18:44
我正在嘗試從 S3 對(duì)象中獲取數(shù)據(jù)并將其放入數(shù)組中。我計(jì)劃通過這個(gè)數(shù)組映射并在網(wǎng)格/列表中的 React 前端顯示數(shù)據(jù)。不過,我正在為嵌套函數(shù)而苦苦掙扎,因此我將不勝感激。const dataFromS3 = async (bucket, file) => {let lines = [];const options = { Bucket: bucket, Key: file };s3.getObject(options, (err, data) => {if (err) { console.log(err);} else { let objectData = data.Body.toString('utf-8'); lines.push(objectData); console.log(lines); return lines;} });};格式化有點(diǎn)奇怪,但這是我從 s3 獲取數(shù)據(jù)的功能。我想以數(shù)組的形式獲取此函數(shù)的輸出并將其傳遞給我正在測(cè)試的“/”路由:app.get('/', async (req, res, next) => {try { let apolloKey = await dataFromS3(s3Bucket, apolloKeywords); res.send(apolloKey); } catch (err) { console.log('Error: ', err); }});似乎s3.getObject函數(shù)中的行的返回值需要在第一個(gè)函數(shù)中返回,以便我可以在 app.get 中訪問它,但經(jīng)過一些嘗試后我似乎無法做到這一點(diǎn)。如果我在datafromS3()的末尾返回它,那么行中的值將變成一個(gè)空數(shù)組,并且我找不到返回它的方法。我也嘗試使用此處找到的方法使用 Promise - How to get response from S3 getObject in Node.js? 但我得到一個(gè) TypeError: Converting Circular Structure to JSON ...
1 回答

jeck貓
TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊
你需要讓你的 dataFromS3 像 htis 一樣運(yùn)行。你沒有從中返回任何東西。AWS 還提供了基于 Promise 的功能。
const dataFromS3 = async (bucket, file) => {
const lines = [];
const options = {
"Bucket": bucket,
"Key": file
};
const data = await s3.getObject(options).promise();
const objectData = data.Body.toString("utf-8");
lines.push(objectData); // You might need to conversion here using JSON.parse(objectData);
console.log(lines);
return lines;
};
添加回答
舉報(bào)
0/150
提交
取消