gpt4 book ai didi

javascript - Firebase getSignedUrl 循环内

转载 作者:行者123 更新时间:2023-12-02 21:53:48 27 4
gpt4 key购买 nike

我需要获取“bucket/loads/:loadID”路径中所有文件的 URL。我能够在名为"file"的数组中获取这些文件。然后我过滤它(我得到 endFiles 数组)。现在我只需要一个名为 url 的新数组来将所有 url 推送到 (getSignedUrl) 中。但我不知道该怎么做。我需要在循环(endFiles.forEach)内获取签名的网址并将其推送到网址数组或类似的东西。

exports.testCloudFunc = functions.storage.object().onFinalize(async object => {
const filePath = object.name;

const { Logging } = require('@google-cloud/logging');
console.log(`Logged: FILEPATH: ${filePath}`);
const id = filePath.split('/');
console.log(`Logged: ID: ${id[0]}/${id[1]}`);
const bucket = object.bucket;
console.log(`Logged: BUCKET: ${object.bucket}`);

async function listFilesByPrefix() {
const options = {
prefix: id[0] + '/' + id[1]
};
const [files] = await storage.bucket(bucket).getFiles(options);

const endFiles = files.filter(el => {
return (
el.name === id[0] + '/' + id[1] + '/' + 'invoiceReport.pdf' ||
el.name === id[0] + '/' + id[1] + '/' + 'POD.pdf' ||
el.name === id[0] + '/' + id[1] + '/' + 'rateConfirmation.pdf'
);
});

for (let i = 0; i < endFiles.length; i++) {
console.log(endFiles[i].name);
}
}
listFilesByPrefix().catch(console.error);
});

我被困住了,需要帮助。非常感谢任何帮助。

最佳答案

getSignedUrl()方法是异步的并返回一个 Promise。

由于要同时执行对此方法的多个调用,因此需要使用Promise.all()如下:

  async function listFilesByPrefix() {
const options = {
prefix: id[0] + '/' + id[1]
};
const [files] = await storage.bucket(bucket).getFiles(options);

const endFiles = files.filter(el => {
return (
el.name === id[0] + '/' + id[1] + '/' + 'invoiceReport.pdf' ||
el.name === id[0] + '/' + id[1] + '/' + 'POD.pdf' ||
el.name === id[0] + '/' + id[1] + '/' + 'rateConfirmation.pdf'
);
});

const config = {
action: 'read',
expires: '03-17-2025'
};

const promises = [];
for (let i = 0; i < endFiles.length; i++) {
console.log(endFiles[i].name);
promises.push(endFiles[i].getSignedUrl(config));
}

const urlsArray = await Promise.all(promises);

return urlsArray;
}


listFilesByPrefix()
.then(results => {
//results is an array of signed URLs
//It's worth noting that values in the array will be in order of the Promises passed with promises.push()
//do whatever you need, for example:
results.forEach(url => {
//....
});
})

关于javascript - Firebase getSignedUrl 循环内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60056973/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com