将图像从Google Cloud功能上传到云端存储

编程入门 行业动态 更新时间:2024-10-06 13:18:37
本文介绍了将图像从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

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功能上传到云端存储

本文发布于:2023-11-26 18:52:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634771.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:云端   图像   功能   Cloud   Google

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!