将图像从Google Cloud功能上传到云端存储 [英] Upload Image from Google Cloud Function to Cloud Storage

查看:286
本文介绍了将图像从Google Cloud功能上传到云端存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google Cloud功能处理文件上传。此功能使用Busboy解析多部分表单数据,然后上传到Google Cloud Storage。

I'm attempting to handle file uploads using a Google Cloud Function. This function uses Busboy to parse the multipart form data and then upload to Google Cloud Storage.

我一直收到同样的错误:错误:{错误:ENOENT:没有这样的文件或目录,在触发函数时打开'/tmp/xxx.png'错误。

I keep receiving the same error: ERROR: { Error: ENOENT: no such file or directory, open '/tmp/xxx.png' error when triggering the function.

错误似乎是当storage.bucket.upload(文件)尝试打开文件路径 /tmp/xxx.png 完成回调函数内发生$ c>。

The error seems to occur within the finish callback function when storage.bucket.upload(file) attempts to open the file path /tmp/xxx.png.

请注意,我无法按此问题,因为调用此应用程序的应用程序是外部非用户应用程序。我也无法直接上传到GCS,因为我需要根据一些请求元数据制作自定义文件名。我应该只使用Google App Engine吗?

Note that I can't generate a signed upload URL as suggested in this question since the application invoking this is an external, non-user application. I also can't upload directly to GCS since I'll be needing to make custom filenames based on some request metadata. Should I just be using Google App Engine instead?

功能代码:

const path = require('path');
const os = require('os');
const fs = require('fs');
const Busboy = require('busboy');
const Storage = require('@google-cloud/storage');
const _ = require('lodash');

const projectId = 'xxx';
const bucketName = 'xxx';


const storage = new Storage({
  projectId: projectId,
});

exports.uploadFile = (req, res) => {
    if (req.method === 'POST') {
        const busboy = new Busboy({ headers: req.headers });
        const uploads = []
        const tmpdir = os.tmpdir();

        busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
            const filepath = path.join(tmpdir, filename)
            var obj = {
                path: filepath, 
                name: filename
            }
            uploads.push(obj);

            var writeStream = fs.createWriteStream(obj.path);
            file.pipe(writeStream);
        });

        busboy.on('finish', () => {
            _.forEach(uploads, function(file) {

                storage
                .bucket(bucketName)
                .upload(file.path, {name: file.name})
                .then(() => {
                  console.log(`${file.name} uploaded to ${bucketName}.`);
                })
                .catch(err => {
                  console.error('ERROR:', err);
                });


                fs.unlinkSync(file.path);
            })

            res.end()
        });

        busboy.end(req.rawBody);
    } else {
        res.status(405).end();
    }
}


推荐答案

我最终放弃了使用Busboy。最新版本的Google Cloud Functions支持Python和Node 8.在节点8中,我只是将所有内容放入async / await函数中,它运行正常。

I eventually gave up on using Busboy. The latest versions of Google Cloud Functions support both Python and Node 8. In node 8, I just put everything into async/await functions and it works fine.

这篇关于将图像从Google Cloud功能上传到云端存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆