使用Node JS将图像从本地目录上传到Google Cloud Storage

时间:2020-09-30 20:09:46

标签: node.js google-cloud-platform

我正在使用Node JS / Express,并且想从url下载文件到本地系统,然后在下一步将其上传到Google Cloud Storage。

这是我的带有中间件的路由器:

  router.post("", fileFromUrl, uploadFromUrl, scrapeController.scrapeCreateOne);

这是一个fileFromUrl中间件,它只是将文件从url保存到本地磁盘

module.exports = (req, res, next) => {
try {
    console.log('Image: ', req.body.image);

    const url = req.body.image ? req.body.image : '';
    console.log(typeof url);

    if(url === '') {
        //no image url provided
        console.log('image parsing skipped');
        next()
    }
    else {
         // image url ok then 
        const pathToImage = path.resolve(__dirname, '..', 'images', Date.now() + '_file.jpg');
        const localPath = fs.createWriteStream(pathToImage);
        
        const saveFile = https.get(url, (response) => {
            console.log(response.headers['content-type']);
            response.pipe(localPath);
        })

        req.body.customImageUrl = pathToImage;
        req.body.customImageName = path.basename(pathToImage);
        
        next();
    }

}
catch (error) {
    console.log(error)
}

}

这是uploadFromUrl中间件,应将文件从本地路径上传到Google Cloud Storage

module.exports = (req, res, next) => {
try {
    console.log(req.body.customImageUrl);
    console.log(req.body.customImageName);
    //storage.getBuckets().then(x => console.log(x));
    //storage.createBucket(`testbucket_${Date.now()}`); / // it does work
    storage.bucket(bucketName).upload(req.body.customImageUrl, {
        gzip: true,
        metadata: {
        cacheControl: 'public, max-age=31536000',
        },
    }).then(
        req.body.customData = `https://storage.googleapis.com/${bucketName}/${req.body.customImageName}`
    ); 
    next();
}
catch (error) {
    res.send(error).json({
        message: 'Error upload middleware' + error,
    });
}

}

现在要做的只是将几乎20kB的空文件上传到Google Cloud Platform,而不是完整图片。我觉得我没有为uploadFromUrl中间件提供适当的文件对象。另一方面,要上传文件的GCP API只是要求提供所提供文件的路径。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

问题是我试图将图像上传到GCP,即使该图像尚未完全保存在服务器上。解决方案是在我提出的将其保存到本地的请求中等待“完成”事件

 const pathToImage = path.resolve(__dirname, '..', 'images', Date.now() + '_file.jpg');
        const localPath = fs.createWriteStream(pathToImage);
        const fileRelativePath = "images/" + path.basename(pathToImage);
        const request = https.get(url, (response) => {
            //console.log(response.headers['content-type']);
            response.pipe(localPath).on('finish', () => {
                console.log('image saved on the server');
                req.body.customImagePath = fileRelativePath;
                req.body.customImageName = path.basename(pathToImage);
                // now you can go and upload the image to GCP in the next moddleware
                next();
            });
        });