Node.js将图像上载到firebase存储
我正在尝试在firebase cloud函数中上载Node.js将图像上载到firebase存储,node.js,firebase,google-cloud-platform,google-cloud-functions,Node.js,Firebase,Google Cloud Platform,Google Cloud Functions,我正在尝试在firebase cloud函数中上载文件(图像)。在阅读了许多帖子后,我发现我唯一能做到这一点的方法就是使用谷歌云平台(GCP)API,下面是我的代码: const storage = require('@google-cloud/storage')(); var bucket = storage.bucket('myBucketName'); var buffer = bufferRepresentMyImage; // what next? 我从GCP的API中找到了以下代码
文件(图像)
。在阅读了许多帖子后,我发现我唯一能做到这一点的方法就是使用谷歌云平台(GCP)API
,下面是我的代码:
const storage = require('@google-cloud/storage')();
var bucket = storage.bucket('myBucketName');
var buffer = bufferRepresentMyImage;
// what next?
我从GCP的API中找到了以下代码:
bucket.upload(filename, (err, file) => {
// code goes here
});
然而,它似乎不起作用,因为它从不要求文件的数据,相反,
回调函数
返回一个文件
。有谁能告诉我如何使用bucket
上传图像
?选项参数中包含远程目标文件名<代码>文件名在您的示例(以及这个答案)中是本地文件名路径
let options = {
destination: fileNameOnDestinationBucket,
metadata: { // (optional)
metadata: yourCustomMetadata,
contentType: 'image/jpeg' // (example)
}
};
// with Promise:
bucket.upload(filename, options).then((response)=>{
...
});
// with callback:
bucket.upload(filename, options, (err, metadata, response)=>{
....
});
你应该看看