gpt4 book ai didi

google-cloud-functions - 如何通过 HTTP Google Cloud Function 上传图片文件?

转载 作者:行者123 更新时间:2023-12-04 08:50:57 27 4
gpt4 key购买 nike

我已阅读有关如何将图像上传到存储桶并通过后台功能进行后期处理的教程。但我的要求是上传图片,进行后期处理,并通过 HTTP 函数立即返回结果。请让我知道这是否是正确的做法,因为我在网上没有得到太多关于此的 Material 。这是我如何去做的:

HTTP 云功能:

exports.uploadImage = function (req, res){
var file = req.body.file;
uploadSomewhere(file)(); < post-processing code which is working fine >

界面形式:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
<script src="http://malsup.github.com/jquery.form.js"></script>
<form id="myForm" action="<cloud_function_url>/uploadImage" method="post">
<label for="file">Choose file to upload</label>
<input type="file" id="file" name="file" multiple>
<input type="submit" value="Submit" />
</form>

<script>
$(document).ready(function() {
$('#myForm').ajaxForm(function() {
});
});
</script>

问题是,在部署函数后,当我从函数所在的文件夹上传图像时,图像被上传。但是如果我从任何其他位置上传图像,它会返回错误:

错误:上传图像文件时出错.... - 错误:ENOENT:没有这样的文件或目录,打开“....”

请让我知道我做错了什么,或者如果您需要更多信息。

最佳答案

这里有用于将文件直接上传到 Cloud Functions 的示例代码:https://cloud.google.com/functions/docs/writing/http#multipart_data_and_file_uploads ,但请注意,使用此方法时存在文件大小限制 (10MB)。如果您的文件比这更大,我建议您使用 Cloud Storage 方法并使用类似 Firebase Realtime Database 的内容。管理客户端的状态。

因此,您使用 Cloud Functions 生成签名上传 URL,然后使用 RTDB 跟踪客户端的进度。返回 URL 和对数据库中要跟踪进度的位置的引用。客户端将监视此位置以获取更新。然后客户端将文件上传到 Cloud Storage 并触发第二个函数。后处理后,它更新 RTDB,将更新推送到客户端。从客户端的角度来看,这都是同步的,但它实际上是一系列异步操作,在数据库中进行状态合并。

如果文件大小小于 10MB,这里是从文档中获取的内联更新的示例代码:

/**
* Parses a 'multipart/form-data' upload request
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
const path = require('path');
const os = require('os');
const fs = require('fs');

// Node.js doesn't have a built-in multipart/form-data parsing library.
// Instead, we can use the 'busboy' library from NPM to parse these requests.
const Busboy = require('busboy');

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

// This object will accumulate all the fields, keyed by their name
const fields = {};

// This object will accumulate all the uploaded files, keyed by their name.
const uploads = {};

// This code will process each non-file field in the form.
busboy.on('field', (fieldname, val) => {
// TODO(developer): Process submitted field values here
console.log(`Processed field ${fieldname}: ${val}.`);
fields[fieldname] = val;
});

let fileWrites = [];

// This code will process each file uploaded.
busboy.on('file', (fieldname, file, filename) => {
// Note: os.tmpdir() points to an in-memory file system on GCF
// Thus, any files in it must fit in the instance's memory.
console.log(`Processed file ${filename}`);
const filepath = path.join(tmpdir, filename);
uploads[fieldname] = filepath;

const writeStream = fs.createWriteStream(filepath);
file.pipe(writeStream);

// File was processed by Busboy; wait for it to be written to disk.
const promise = new Promise((resolve, reject) => {
file.on('end', () => {
writeStream.end();
});
writeStream.on('finish', resolve);
writeStream.on('error', reject);
});
fileWrites.push(promise);
});

// Triggered once all uploaded files are processed by Busboy.
// We still need to wait for the disk writes (saves) to complete.
busboy.on('finish', () => {
Promise.all(fileWrites)
.then(() => {
// TODO(developer): Process saved files here
for (const name in uploads) {
const file = uploads[name];
fs.unlinkSync(file);
}
res.send();
});
});

busboy.end(req.rawBody);
} else {
// Return a "method not allowed" error
res.status(405).end();
}
};

关于google-cloud-functions - 如何通过 HTTP Google Cloud Function 上传图片文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46511848/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com