我无法使用云功能将字节数组格式的图像文件上传到firestore。下面的代码在app.listen()的本地工作正常但是当我尝试使用导出功能时它无法正常工作
const functions = require('firebase-functions');
const { Storage } = require('@google-cloud/storage');
const express = require('express');
var app = express();
var fs = require('fs');
// Your Google Cloud Platform project ID
const projectId ='<project-Id>';
// Creates a client
const storage = new Storage({
projectId: projectId,
keyFilename: '<>'
});
const bucket = storage.bucket("<bucket-name>");
// Byte aray to Image
function saveImage(filename, data) {
console.log("inside saveImage", data.length);
var myBuffer = new Buffer.alloc(data.length);
for (var i = 0; i < data.length; i++) {
myBuffer[i] = data[i];
}
console.log(myBuffer)
fs.createWriteStream(filename, myBuffer, 'binary', function (err) {
console.log("inside writeFile");
if (err) {
console.log('error inside fs', err);
} else {
console.log("The file was saved!");
}
});
}
saveImage("image1.png", result);
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,
Content-Type, Accept");
next();
});
exports.UploadImage = functions.https.onRequest(app);
有人可以帮帮我吗。提前致谢
这是我使用Firebase功能和Google Cloud Pub / Sub将文件上传到Firebase存储时的最终解决方案。
注意Pub / Sub是可选的,仅适用于我的用例
const functions = require('firebase-functions');
const admin = require('firebase-admin');
function saveImage(filename, data) {
const bucket = admin.storage().bucket();
const imageBuffer = new Uint8Array(data);
const file = bucket.file(`optional_sub_directory/${filename}`, {
uploadType: {resumable: false}
});
file.save(imageBuffer, (err) => {
if (err) {
console.error(`Error uploading: ${filename} with message: ${err.message}`);
return;
}
console.log('Uploaded file');
});
}
exports.helloPubSub = functions.pubsub
.topic('demo-topic')
.onPublish(async(message) => {
/**
* Here goes code to extract the file buffer
*/
const buffer = /** buffer */
saveImage("stack_overflow.txt", buffer);
});
一些阅读后,图像上传到firebase存储。我意识到,不是将字节数组转换为Image,我们可以使用Uint8Array()直接上传字节数组,自动将其转换为图像并上传到firebase存储中。以下是修改后的代码。
const functions = require('firebase-functions');
const { Storage } = require('@google-cloud/storage');
const express = require('express');
var app = express();
// Your Google Cloud Platform project ID
const projectId ='<project-Id>';
// Creates a client
const storage = new Storage({
projectId: projectId,
keyFilename: './servicekey.json'
});
const bucket = storage.bucket("<bucket-name>");
const options = {
action: 'read',
expires: '03-17-2025'
};
// storing Byte array
function saveImage(filename, data) {
var imageBuffer = new Uint8Array(data);
var file = bucket.file(filename);
file.save(imageBuffer, {
metadata: { contentType: 'image/png' },
},
((error) => {
if (error) {
console.log('error')
}
file.getSignedUrl(options)
.then(results => {
const url = results[0];
console.log(`The signed url for ${imageName} is ${url}.`);
})
}));
}
saveImage("image1.png", <byte-Array as input>);
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,
Content-Type, Accept");
next();
});
exports.UploadImage = functions.https.onRequest(app);
谢谢你的回复。我使用相同的方法,但我有两个问题。首先是虽然正在上传图像但是在运行该功能时我没有得到响应。另一个问题是我在下载后和Firebase页面上看不到图像。我已将ContentType设置为'image / jpg'。
这是我的代码:
const bucket = admin.storage().bucket();
try {
const imageBuffer = new Uint8Array(req.rawBody);
const file = bucket.file(`projects/sample.jpg`);
file.save(
imageBuffer,
{ resumable: false, metadata: { contentType: "image/jpg" } },
err => {
if (err) {
throw new Error(err);
}
res.send({
success: true,
message: "File Successfully Uploaded..."
});
}
);
} catch (error) {
throw new Error(error);
}