Upload Image from Google Cloud Function to Cloud Storage

♀尐吖头ヾ 提交于 2019-12-23 15:44:11

问题


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.

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

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

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?

Function code:

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();
    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/51297006/upload-image-from-google-cloud-function-to-cloud-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!