Node.js Node JS-如何等到Google云存储桶上存在文件,然后下载文件

Node.js Node JS-如何等到Google云存储桶上存在文件,然后下载文件,node.js,google-cloud-platform,google-cloud-storage,Node.js,Google Cloud Platform,Google Cloud Storage,我和我的团队已经尝试了许多方法来解决这个问题,但是每当我们去下载文件时,我们都会收到一个错误,说明文件不存在 这样做的典型方式是什么。这似乎很容易,但我似乎找不到任何有效的方法 为了澄清这一点,我的节点JS运行一个函数,将文件上传到一个bucket。这将触发一个云函数,该函数执行一些处理,然后将文件上载到另一个bucket。我希望Node JS等待上传另一个文件,然后下载该文件 const storage = new Storage(); const bucket = storage.bucke

我和我的团队已经尝试了许多方法来解决这个问题,但是每当我们去下载文件时,我们都会收到一个错误,说明文件不存在

这样做的典型方式是什么。这似乎很容易,但我似乎找不到任何有效的方法

为了澄清这一点,我的节点JS运行一个函数,将文件上传到一个bucket。这将触发一个云函数,该函数执行一些处理,然后将文件上载到另一个bucket。我希望Node JS等待上传另一个文件,然后下载该文件

const storage = new Storage();
const bucket = storage.bucket('receipttext');


async function uploadFile() {
    await storage.bucket("BUCKET_ONE").upload('FILE_PATH', {
        gzip: true,
        metadata: {
            cacheControl: 'public, max-age=31536000',
        },
    });
    console.log('step1!');
    const options = {
        destination: 'DOWNLOAD_PATH',
    };

    //Something needs to go here to cause Node JS to wait until the file exists
    let file = null;
    let ifExist = false;
    while(!ifExist) {
        console.log('step1.1!');
        file = await storage
            .bucket("BUCKET_TWO")
            .file('FILE_NAME');
        ifExist =  file.exists();
        //console.log('step1.2!',ifExist);
        console.log('step2!');
    }


    //Downloads the File
    await file.download(options).catch(function(error){
            console.log(2);
        });
    console.log(
        `gs:// downloaded .`
    );
    console.log('step3!');
    let obj = JSON.parse(fs.readFileSync('DOWNLOAD_PATH', 'utf8'));
    console.log(obj,"here");

    res.json({response: obj});
}
uploadFile().catch(console.error);
console.log("successUpload");

您唯一缺少的是wait关键字as方法返回一个promise(作为布尔值始终为true),它解析为一个数组,其中的第一个元素是您需要的布尔值

所以我会像这样调整线条:

ifExist =  await file.exists()[0];

也许这会有所帮助:您可以尝试使用云函数,尝试由存储中的创建事件触发。在该函数中,您可以实现下载端。我不确定这是否符合您的实际目的,但这是一种在文件上传到bucket时下载文件的方法