sei*_*ida 3 node.js google-cloud-storage firebase google-cloud-functions
我想从可调用的 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!");
});
Run Code Online (Sandbox Code Playgroud)
你能帮我理解,该怎么做吗?
太感谢了
更新: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: /sf/ask/3777862791/
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!");
});
Run Code Online (Sandbox Code Playgroud)
有可能有有效的例子吗?
喜欢这个页面: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) {
});
Run Code Online (Sandbox Code Playgroud)
毕竟我的存储空间还是空的。
我花了一段时间才弄清楚,所以将其发布在这里,以防它对其他有同样想法的人有所帮助。我正在使用强大的无服务器来解析请求。
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");
});
});
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4228 次 |
| 最近记录: |