从可调用的https云函数将图像上载到Firebase Storage



我想从可调用的https云函数将图像上传到Firebase Storage,但我不知道如何做到这一点。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const storageBucket = admin.storage().bucket('gs://xxxxx.appspot.com');
exports.test_upload = functions.https.onRequest(async (request, response) => {
var base64Img = "data:image/jpeg;base64,/9j/4AAQSkZJRg...AoNDQoIC";
let filename = new Date().getTime() + '.jpg';
await storageRef.child('/destination/'+filename).putString(base64Img, 'data_url').then(function(snapshot) {
snapshot.ref.getDownloadURL().then(async (url) => { 

console.log(url);
});
});

response.status(200).send("Done!");
});

你能帮我理解,怎么做吗?

非常感谢

更新:2020/07/22

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const storageBucket = admin.storage().bucket('perfume-group.appspot');
const {Storage} = require('@google-cloud/storage');
const storage = new Storage({keyFilename: 'perfume-group-key.json'});
exports.test_upload = functions.https.onRequest(async (request, response) => {
/// CAS 1: Don't works
// Source: https://stackoverflow.com/questions/53969468/how-to-store-files-in-firebase-using-node-js
const fileUrl = 'https://i.pinimg.com/originals/4a/9d/f8/4a9df83774219e0d02bbb9e5dc9be125.jpg';
const opts = {
destination: 'destination/file1.jpg',
metadata: {
contentType: 'image/jpeg'
}
};
firebase.storage().bucket().upload(fileUrl, opts);
//////////////////////////////////////////////////
/// CASE 2: Don't works
const bucket = storage.bucket('perfume-group.appspot.com');
bucket.upload(fileUrl, {
destination: 'destination/file2.jpg',
metadata: {
contentType: 'image/jpeg'
}
});
//////////////////////////////////////////////////
/// CASE 3: Don't works
const options = {
destination: 'destination/file3.jpg',
};
// Downloads the file
await storage.bucket('perfume-group.appspot.com').file(fileUrl).download(options);
//////////////////////////////////////////////////

response.status(200).send("Done!");
});

有可行的例子吗
类似于此页面:https://firebase.google.com/docs/storage/web/upload-files

我知道如何将数据添加到数据库,但将文件添加到存储还不清楚。

为什么这种解释不起作用?->https://googleapis.dev/nodejs/storage/latest/Bucket.html#upload

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('albums');
await bucket.upload('https://i.pinimg.com/originals/4a/9d/f8/4a9df83774219e0d02bbb9e5dc9be125.jpg', function(err, file, apiResponse) {

});

毕竟,我的储藏室还是空的。

我花了一段时间才弄清楚,所以把它发布在这里,以防对其他同病相怜的人有所帮助。我正在使用强大的无服务器来解析请求。

const functions = require("firebase-functions");
const formidable = require("formidable-serverless");
const admin = require("firebase-admin");
admin.initializeApp();
exports.uploadFile = functions.https.onRequest((req, res) => {
var form = new formidable.IncomingForm();
form.parse(req, async (err, fields, files) => {
var file = files.fileToUpload;
if (err || !file) {
res.status(500).send(err);
return;
}
var filePath = file.path;
var filename = file.name;
var bucket = admin.storage().bucket("nameOfBucket.appspot.com");
const options = {
destination: "profile/" + filename,
contentType: "image/jpeg", //file.path
};
//upload image
await bucket
.upload(output, options)
.catch((error) => res.status(500).send(error));
await res.status(200).send("success");
});
});

Html表单代码:

<form action="https://yourCloudFunctionsUrl.cloudfunctions.net/uploadFile" method="post" enctype="multipart/form-data">
<input type="file" accept="image/*" name="fileToUpload" >
<input type="submit" value="upload">
</form>

相关内容

  • 没有找到相关文章

最新更新